XPathNavigator.Select 方法

定義

使用指定的 XPath 表達式選擇節點集合。

多載

名稱 Description
Select(String)

使用指定的 XPath 表達式選擇節點集合。

Select(XPathExpression)

利用指定的 XPathExpression節點集合選擇。

Select(String, IXmlNamespaceResolver)

使用指定的 XPath 表達式選擇節點集合,並 IXmlNamespaceResolver 指定物件以解析命名空間前綴。

Select(String)

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

使用指定的 XPath 表達式選擇節點集合。

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

參數

xpath
String

A String 代表 XPath 表達式。

傳回

XPathNodeIterator指向所選節點集合。

例外狀況

XPath 表達式包含錯誤或其回傳類型不是節點集合。

XPath 表達式無效。

範例

以下範例使用該 Select 方法來選擇節點集合。

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathNodeIterator nodes = navigator.Select("/bookstore/book");
nodes.MoveNext();
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
    Console.WriteLine(nodesText.Current.Value);
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")
nodes.MoveNext()
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

此範例會將 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>

備註

選擇的上下文是該方法被呼叫時的位置 XPathNavigator 。 呼叫此方法後, XPathNodeIterator 返回的代表所選節點的集合。 使用 MoveNext 的方法 XPathNodeIterator 來遍歷所選節點集。

以下 C# 程式碼會遍歷所選節點集合。

XPathNodeIterator iterator = nav.Select("/bookstore/book");
while (iterator.MoveNext())
{
    Console.WriteLine(Iterator.Current.Name);
}

以下是使用此 Select 方法時需注意的重要事項。

另請參閱

適用於

Select(XPathExpression)

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

利用指定的 XPathExpression節點集合選擇。

public:
 virtual System::Xml::XPath::XPathNodeIterator ^ Select(System::Xml::XPath::XPathExpression ^ expr);
public virtual System.Xml.XPath.XPathNodeIterator Select(System.Xml.XPath.XPathExpression expr);
abstract member Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
override this.Select : System.Xml.XPath.XPathExpression -> System.Xml.XPath.XPathNodeIterator
Public Overridable Function Select (expr As XPathExpression) As XPathNodeIterator

參數

expr
XPathExpression

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

傳回

指向 XPathNodeIterator 所選節點集合的 。

例外狀況

XPath 表達式包含錯誤或其回傳類型不是節點集合。

XPath 表達式無效。

範例

以下範例使用該 Select 方法來選擇節點集合。

XPathDocument document = new XPathDocument("books.xml");
XPathNavigator navigator = document.CreateNavigator();

XPathExpression query = navigator.Compile("/bookstore/book");
XPathNodeIterator nodes = navigator.Select(query);
XPathNavigator nodesNavigator = nodes.Current;

XPathNodeIterator nodesText = nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

while (nodesText.MoveNext())
{
    Console.WriteLine(nodesText.Current.Value);
}
Dim document As XPathDocument = New XPathDocument("books.xml")
Dim navigator As XPathNavigator = document.CreateNavigator()

Dim query As XPathExpression = navigator.Compile("/bookstore/book")
Dim nodes As XPathNodeIterator = navigator.Select(query)
Dim nodesNavigator As XPathNavigator = nodes.Current

Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

While nodesText.MoveNext()
    Console.WriteLine(nodesText.Current.Value)
End While

此範例會將 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>

備註

選擇的上下文是你呼叫此方法時的位置 XPathNavigator 。 呼叫此方法後,return 代表 XPathNodeIterator 所選節點的集合。 在 上 MoveNextXPathNodeIterator遍歷所選節點集。

以下 C# 程式碼會遍歷所選節點集合。

XPathNodeIterator ni = nav.Select(expr);
while (ni.MoveNext())
{
    Console.WriteLine(ni.Current.Name);
}

以下是使用此 Select 方法時需注意的重要事項。

例如,假設文件包含以下 XML 節點。

<bookstore xmlns:bk='urn:samples'>
    <book bk:ISBN='1-325-0980'>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

此時,以下 C# 程式碼選擇節點。bk:ISBN

XPathExpression expr = nav.Compile("book/@bk:ISBN");
XmlNamespaceManager mngr = new XmlNamespaceManager(new NameTable());
mngr.AddNamespace("bk","urn:samples");
expr.SetContext(mngr);
XPathNodeIterator ni = nav.Select(expr);

Note

若不 XPathExpression 包含前綴,則假設命名空間 URI 為空命名空間。 如果你的 XML 包含預設命名空間,你仍必須使用該 SetContext 方法,並提供包含前綴和命名空間 URI 的 URI XmlNamespaceManager 來處理預設命名空間。

舉例來說,假設你有以下的 XML。

<bookstore xmlns="http://www.lucernepublishing.com">
    <book>
        <title>Pride And Prejudice</title>
    </book>
</bookstore>

此時,以下 C# 程式碼會選擇所有書籍節點:

XmlNamespaceManager nsmgr = new XmlNamespaceManager(nav.NameTable);
nsmgr.AddNamespace("ab", "http://www.lucernepublishing.com");
XPathExpression expr;
expr = nav.Compile("//ab:book");
expr.SetContext(nsmgr);
XPathNodeIterator ni = nav.Select(expr);

此方法對 的 XPathNavigator狀態沒有影響。

另請參閱

適用於

Select(String, IXmlNamespaceResolver)

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

使用指定的 XPath 表達式選擇節點集合,並 IXmlNamespaceResolver 指定物件以解析命名空間前綴。

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

參數

xpath
String

A String 代表 XPath 表達式。

resolver
IXmlNamespaceResolver

用於解析命名空間前綴的 IXmlNamespaceResolver 物件。

傳回

指向 XPathNodeIterator 所選節點集合的 。

例外狀況

XPath 表達式包含錯誤或其回傳類型不是節點集合。

XPath 表達式無效。

範例

以下範例說明如何使用 Select 方法選擇節點集,並指定 XmlNamespaceManager 物件以解析 XPath 表達式中的命名空間前綴。

XPathDocument document = new XPathDocument("contosoBooks.xml");
XPathNavigator navigator = document.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(navigator.NameTable);
manager.AddNamespace("bk", "http://www.contoso.com/books");

XPathNodeIterator nodes = navigator.Select("/bk:bookstore/bk:book/bk:price", manager);
// Move to the first node bk:price node
if(nodes.MoveNext())
{
    // now nodes.Current points to the first selected node
    XPathNavigator nodesNavigator = nodes.Current;

    //select all the descendants of the current price node
    XPathNodeIterator nodesText =
       nodesNavigator.SelectDescendants(XPathNodeType.Text, false);

    while(nodesText.MoveNext())
    {
       Console.WriteLine(nodesText.Current.Value);
    }
}
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 nodes As XPathNodeIterator = navigator.Select("/bk:bookstore/bk:book/bk:price", manager)
' Move to the first node bk:price node.
If (nodes.MoveNext()) Then
    ' Now nodes.Current points to the first selected node.
    Dim nodesNavigator As XPathNavigator = nodes.Current

    ' Select all the descendants of the current price node.
    Dim nodesText As XPathNodeIterator = nodesNavigator.SelectDescendants(XPathNodeType.Text, False)

    While nodesText.MoveNext()
        Console.WriteLine(nodesText.Current.Value)
    End While
End If

此範例會將 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>

適用於