XPathNavigator.SelectSingleNode Metoda

Definicja

Wybiera jeden węzeł w obiekcie XPathNavigator.

Przeciążenia

SelectSingleNode(String, IXmlNamespaceResolver)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego zapytania XPath z określonym obiektem IXmlNamespaceResolver w celu rozpoznania prefiksów przestrzeni nazw.

SelectSingleNode(String)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego zapytania XPath.

SelectSingleNode(XPathExpression)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego XPathExpression obiektu.

SelectSingleNode(String, IXmlNamespaceResolver)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego zapytania XPath z określonym obiektem IXmlNamespaceResolver w celu rozpoznania prefiksów przestrzeni nazw.

public:
 virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::String ^ xpath, System::Xml::IXmlNamespaceResolver ^ resolver);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (string xpath, System.Xml.IXmlNamespaceResolver? resolver);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (string xpath, System.Xml.IXmlNamespaceResolver resolver);
abstract member SelectSingleNode : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : string * System.Xml.IXmlNamespaceResolver -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (xpath As String, resolver As IXmlNamespaceResolver) As XPathNavigator

Parametry

xpath
String

Reprezentujący String wyrażenie XPath.

resolver
IXmlNamespaceResolver

Obiekt używany do rozpoznawania IXmlNamespaceResolver prefiksów przestrzeni nazw w zapytaniu XPath.

Zwraca

XPathNavigator

Obiekt XPathNavigator , który zawiera pierwszy pasujący węzeł dla określonego zapytania XPath; w przeciwnym razie null , jeśli nie ma żadnych wyników zapytania.

Wyjątki

Napotkano błąd w zapytaniu XPath lub zwracany typ wyrażenia XPath nie jest węzłem.

Zapytanie XPath jest nieprawidłowe.

Przykłady

Poniższy przykład wybiera jeden węzeł z XPathNavigator obiektu na podstawie określonego zapytania XPath i używa określonego XmlNamespaceManager obiektu do rozpoznawania prefiksów przestrzeni nazw w zapytaniu XPath.

XPathDocument^ document = gcnew XPathDocument("contosoBooks.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XmlNamespaceManager^ manager = gcnew XmlNamespaceManager(navigator->NameTable);
manager->AddNamespace("bk", "http://www.contoso.com/books");

XPathNavigator^ node = navigator->SelectSingleNode("//bk:title", manager);
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();

XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

XPathNavigator node = navigator.SelectSingleNode("//bk:title", manager);
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("contosoBooks.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim manager As XmlNamespaceManager = New XmlNamespaceManager(navigator.NameTable)
manager.AddNamespace("bk", "http://www.contoso.com/books")

Dim node As XPathNavigator = navigator.SelectSingleNode("//bk:title", manager)
Console.WriteLine(node.InnerXml)

W przykładzie plik jest contosoBooks.xml pobierany jako dane wejściowe.

<?xml version="1.0" encoding="utf-8" ?>  
<bookstore xmlns="http://www.contoso.com/books">  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Uwagi

Metoda SelectSingleNode wybiera pierwszy węzeł, który jest zgodny z określonym zapytaniem XPath. Metoda SelectSingleNode używa IXmlNamespaceResolver obiektu do rozpoznawania prefiksów przestrzeni nazw.

Uwaga

W programie XPath wszystkie przestrzenie nazw muszą być poprzedzone prefiksem w zapytaniu. Jeśli dokument XML używa domyślnej przestrzeni nazw, nadal musisz dodać prefiks przestrzeni nazw do IXmlNamespaceResolver obiektu.

Dotyczy

SelectSingleNode(String)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego zapytania XPath.

public:
 virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::String ^ xpath);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (string xpath);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (string xpath);
abstract member SelectSingleNode : string -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : string -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (xpath As String) As XPathNavigator

Parametry

xpath
String

Reprezentujący String wyrażenie XPath.

Zwraca

XPathNavigator

Obiekt XPathNavigator , który zawiera pierwszy pasujący węzeł dla określonego zapytania XPath; w przeciwnym razie, null jeśli nie ma żadnych wyników zapytania.

Wyjątki

Napotkano błąd w zapytaniu XPath lub zwracany typ wyrażenia XPath nie jest węzłem.

Zapytanie XPath jest nieprawidłowe.

Przykłady

Poniższy przykład wybiera jeden węzeł z XPathNavigator określonego zapytania XPath.

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathNavigator^ node = navigator->SelectSingleNode("//title");
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNavigator node = navigator.SelectSingleNode("//title");
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim node As XPathNavigator = navigator.SelectSingleNode("//title")
Console.WriteLine(node.InnerXml)

W przykładzie plik jest books.xml pobierany jako dane wejściowe.

<?xml version="1.0" encoding="utf-8" ?>   
<bookstore>  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Uwagi

Metoda SelectSingleNode wybiera pierwszy węzeł, który jest zgodny z zapytaniem XPath w XPathNavigator obiekcie .

Dotyczy

SelectSingleNode(XPathExpression)

Wybiera jeden węzeł w XPathNavigator obiekcie przy użyciu określonego XPathExpression obiektu.

public:
 virtual System::Xml::XPath::XPathNavigator ^ SelectSingleNode(System::Xml::XPath::XPathExpression ^ expression);
public virtual System.Xml.XPath.XPathNavigator? SelectSingleNode (System.Xml.XPath.XPathExpression expression);
public virtual System.Xml.XPath.XPathNavigator SelectSingleNode (System.Xml.XPath.XPathExpression expression);
abstract member SelectSingleNode : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNavigator
override this.SelectSingleNode : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNavigator
Public Overridable Function SelectSingleNode (expression As XPathExpression) As XPathNavigator

Parametry

expression
XPathExpression

XPathExpression Obiekt zawierający skompilowane zapytanie XPath.

Zwraca

XPathNavigator

Obiekt XPathNavigator , który zawiera pierwszy pasujący węzeł dla określonego zapytania XPath; w przeciwnym razie null , jeśli nie ma żadnych wyników zapytania.

Wyjątki

Napotkano błąd w zapytaniu XPath lub zwracany typ wyrażenia XPath nie jest węzłem.

Zapytanie XPath jest nieprawidłowe.

Przykłady

Poniższy przykład wybiera jeden węzeł na XPathNavigator podstawie zapytania XPath zawartego w elemecie XPathExpression.

XPathDocument^ document = gcnew XPathDocument("books.xml");
XPathNavigator^ navigator = document->CreateNavigator();

XPathExpression^ query = navigator->Compile("//title");

XPathNavigator^ node = navigator->SelectSingleNode(query);
Console::WriteLine(node->InnerXml);
XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathExpression query = navigator.Compile("//title");

XPathNavigator node = navigator.SelectSingleNode(query);
Console.WriteLine(node.InnerXml);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim query As XPathExpression = navigator.Compile("//title")

Dim node As XPathNavigator = navigator.SelectSingleNode(query)
Console.WriteLine(node.InnerXml)

W przykładzie plik jest books.xml pobierany jako dane wejściowe.

<?xml version="1.0" encoding="utf-8" ?>   
<bookstore>  
    <book genre="autobiography" publicationdate="1981-03-22" ISBN="1-861003-11-0">  
        <title>The Autobiography of Benjamin Franklin</title>  
        <author>  
            <first-name>Benjamin</first-name>  
            <last-name>Franklin</last-name>  
        </author>  
        <price>8.99</price>  
    </book>  
    <book genre="novel" publicationdate="1967-11-17" ISBN="0-201-63361-2">  
        <title>The Confidence Man</title>  
        <author>  
            <first-name>Herman</first-name>  
            <last-name>Melville</last-name>  
        </author>  
        <price>11.99</price>  
    </book>  
    <book genre="philosophy" publicationdate="1991-02-15" ISBN="1-861001-57-6">  
        <title>The Gorgias</title>  
        <author>  
            <name>Plato</name>  
        </author>  
        <price>9.99</price>  
    </book>  
</bookstore>  

Uwagi

Metoda SelectSingleNode wybiera pierwszy węzeł, który pasuje do skompilowanego zapytania XPath zawartego XPathExpression w obiekcie w obiekcie XPathNavigator.

Dotyczy