共用方式為


XPathNavigator.Select 方法

定義

使用指定的 XPath 運算式來選取節點集。

多載

Select(String)

使用指定的 XPath 運算式來選取節點集。

Select(XPathExpression)

使用指定的 XPathExpression 來選取節點集。

Select(String, IXmlNamespaceResolver)

使用指定的 XPath 運算式並搭配用來解析命名空間前置詞的 IXmlNamespaceResolver,以選取節點集。

Select(String)

使用指定的 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

String 表示 XPath 運算式。

傳回

XPathNodeIterator

XPathNodeIterator 指向選定節點集。

例外狀況

XPath 運算式有錯誤,或其傳回型別不是節點集。

XPath 運算式無效。

範例

下列範例會 Select 使用 方法來選取節點集。

XPathDocument^ document = gcnew 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);
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 回的 代表選取的節點集。 使用 MoveNextXPathNodeIterator 方法來逐一查看選取的節點集。

下列 C# 程式碼會逐一查看選取的節點集。

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

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

另請參閱

適用於

Select(XPathExpression)

使用指定的 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

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

傳回

XPathNodeIterator

指向選定節點集的 XPathNodeIterator

例外狀況

XPath 運算式有錯誤,或其傳回型別不是節點集。

XPath 運算式無效。

範例

下列範例會 Select 使用 方法來選取節點集。

XPathDocument^ document = gcnew 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);
}
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 。 呼叫此方法之後,傳 XPathNodeIterator 回的 代表選取的節點集。 在 MoveNext 上使用 XPathNodeIterator ,逐一查看選取的節點集。

下列 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);  

注意

XPathExpression如果 不包含前置詞,則會假設命名空間 URI 是空的命名空間。 如果您的 XML 包含預設命名空間,您仍 SetContext 必須使用 方法,並提供 XmlNamespaceManager 包含前置詞和命名空間 URI 的 ,以處理預設命名空間。

例如,假設您有下列 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)

使用指定的 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

String 表示 XPath 運算式。

resolver
IXmlNamespaceResolver

IXmlNamespaceResolver 物件,用於解析命名空間前置詞。

傳回

XPathNodeIterator

指向選定節點集的 XPathNodeIterator

例外狀況

XPath 運算式有錯誤,或其傳回型別不是節點集。

XPath 運算式無效。

範例

下列範例說明如何使用 Select 方法選取節點集,並搭配 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");

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);
    }
}
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>  

適用於