共用方式為


XPathNavigator.SelectSingleNode 方法

定義

XPathNavigator 中選取單一節點。

多載

SelectSingleNode(String, IXmlNamespaceResolver)

使用指定的 XPath 查詢並搭配指定的 XPathNavigator 物件,在 IXmlNamespaceResolver 物件中選取單一節點,以解析命名空間前置詞。

SelectSingleNode(String)

使用指定的 XPath 查詢,於 XPathNavigator 中選取單一節點。

SelectSingleNode(XPathExpression)

使用指定的 XPathNavigator 物件,於 XPathExpression 中選取單一節點。

SelectSingleNode(String, IXmlNamespaceResolver)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs

使用指定的 XPath 查詢並搭配指定的 XPathNavigator 物件,在 IXmlNamespaceResolver 物件中選取單一節點,以解析命名空間前置詞。

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

參數

xpath
String

表示 XPath 運算式的 String

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver 物件,用來解析 XPath 查詢中的命名空間前置詞。

傳回

XPathNavigator 物件,包含指定 XPath 查詢的第一個相符節點;如果沒有查詢結果,則為 null

例外狀況

在 XPath 查詢中遇到了錯誤,或是 XPath 運算式的傳回型別不是節點。

XPath 查詢無效。

範例

下列範例會根據指定的 XPath 查詢,從 XPathNavigator 物件中選取單一節點,並使用 XmlNamespaceManager 指定的 物件解析 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)

範例將 contosoBooks.xml 檔案做為輸入。

<?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>  

備註

方法 SelectSingleNode 會選取符合指定 XPath 查詢的第一個節點。 方法 SelectSingleNodeIXmlNamespaceResolver 使用 物件來解析命名空間前置詞。

注意

在 XPath 中,所有命名空間都必須在查詢中加上前置詞。 如果 XML 檔使用預設命名空間,您仍必須將命名空間前置詞新增至 IXmlNamespaceResolver 物件。

適用於

SelectSingleNode(String)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs

使用指定的 XPath 查詢,於 XPathNavigator 中選取單一節點。

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

參數

xpath
String

表示 XPath 運算式的 String

傳回

XPathNavigator 物件,包含指定 XPath 查詢的第一個相符節點;如果沒有查詢結果,則為 null

例外狀況

在 XPath 查詢中遇到了錯誤,或是 XPath 運算式的傳回型別不是節點。

XPath 查詢無效。

範例

下列範例會根據指定的 XPath 查詢,從 XPathNavigator 中選取單一節點。

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)

範例將 books.xml 檔案做為輸入。

<?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>  

備註

方法 SelectSingleNode 會選取符合 物件中 XPathNavigator XPath 查詢的第一個節點。

適用於

SelectSingleNode(XPathExpression)

來源:
XPathNavigator.cs
來源:
XPathNavigator.cs
來源:
XPathNavigator.cs

使用指定的 XPathNavigator 物件,於 XPathExpression 中選取單一節點。

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

參數

expression
XPathExpression

包含已編譯 XPath 查詢的 XPathExpression 物件。

傳回

XPathNavigator 物件,包含指定 XPath 查詢的第一個相符節點;如果沒有查詢結果,則為 null

例外狀況

在 XPath 查詢中遇到了錯誤,或是 XPath 運算式的傳回型別不是節點。

XPath 查詢無效。

範例

下列範例會根據 中包含的 XPathExpression XPath 查詢,從 XPathNavigator 中選取單一節點。

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)

範例將 books.xml 檔案做為輸入。

<?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>  

備註

方法 SelectSingleNode 會選取符合 物件中所 XPathExpressionXPathNavigator 包含編譯 XPath 查詢的第一個節點。

適用於