XPathNavigator.Select Metoda

Definicja

Wybiera zestaw węzłów przy użyciu określonego wyrażenia XPath.

Przeciążenia

Select(String)

Wybiera zestaw węzłów przy użyciu określonego wyrażenia XPath.

Select(XPathExpression)

Wybiera zestaw węzłów przy użyciu określonego XPathExpressionelementu .

Select(String, IXmlNamespaceResolver)

Wybiera zestaw węzłów przy użyciu określonego wyrażenia XPath z określonym obiektem IXmlNamespaceResolver w celu rozpoznawania prefiksów przestrzeni nazw.

Select(String)

Wybiera zestaw węzłów przy użyciu określonego wyrażenia 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

Parametry

xpath
String

Wyrażenie String XPath reprezentujące.

Zwraca

XPathNodeIterator

Wskazanie XPathNodeIterator wybranego zestawu węzłów.

Wyjątki

Wyrażenie XPath zawiera błąd lub jego zwracany typ nie jest zestawem węzłów.

Wyrażenie XPath jest nieprawidłowe.

Przykłady

W poniższym przykładzie użyto Select metody do wybrania zestawu węzłów.

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

W przykładzie books.xml plik jest 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

Kontekst zaznaczenia jest pozycją XPathNavigator , w przypadku wywołania tej metody. Po wywołaniu tej metody zwrócony XPathNodeIterator reprezentuje zestaw wybranych węzłów. Użyj MoveNext metody XPathNodeIterator , aby iterować w wybranym zestawie węzłów.

Poniższy kod języka C# iteruje wybrany zestaw węzłów.

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

Poniżej przedstawiono ważne uwagi, które należy wziąć pod uwagę podczas korzystania z Select metody .

Zobacz też

Dotyczy

Select(XPathExpression)

Wybiera zestaw węzłów przy użyciu określonego XPathExpressionelementu .

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

Parametry

expr
XPathExpression

XPathExpression Obiekt zawierający skompilowane zapytanie XPath.

Zwraca

XPathNodeIterator

Element XPathNodeIterator wskazujący wybrany zestaw węzłów.

Wyjątki

Wyrażenie XPath zawiera błąd lub jego zwracany typ nie jest zestawem węzłów.

Wyrażenie XPath jest nieprawidłowe.

Przykłady

W poniższym przykładzie użyto Select metody do wybrania zestawu węzłów.

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

W przykładzie books.xml plik jest 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

Kontekst zaznaczenia jest pozycją XPathNavigator po wywołaniu tej metody. Po wywołaniu tej metody zwrócony XPathNodeIterator element reprezentuje zestaw wybranych węzłów. Użyj MoveNext polecenia w elemecie XPathNodeIterator , aby iterować wybrany zestaw węzłów.

Poniższy kod języka C# iteruje wybrany zestaw węzłów.

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

Poniżej przedstawiono ważne uwagi, które należy wziąć pod uwagę podczas korzystania z Select metody .

Załóżmy na przykład, że dokument zawiera następujące węzły XML.

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

W tym przypadku poniższy kod języka C# wybiera bk:ISBN węzeł.

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

Uwaga

Jeśli element XPathExpression nie zawiera prefiksu, zakłada się, że identyfikator URI przestrzeni nazw jest pustą przestrzenią nazw. Jeśli kod XML zawiera domyślną przestrzeń nazw, nadal musisz użyć SetContext metody i podać XmlNamespaceManager prefiks i identyfikator URI przestrzeni nazw, aby obsłużyć domyślną przestrzeń nazw.

Załóżmy na przykład, że masz następujący kod XML.

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

W tym przypadku następujący kod w języku C# wybiera wszystkie węzły książki:

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

Ta metoda nie ma wpływu na stan obiektu XPathNavigator.

Zobacz też

Dotyczy

Select(String, IXmlNamespaceResolver)

Wybiera zestaw węzłów przy użyciu określonego wyrażenia XPath z określonym obiektem IXmlNamespaceResolver w celu rozpoznawania prefiksów przestrzeni nazw.

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

Parametry

xpath
String

Wyrażenie String XPath reprezentujące.

resolver
IXmlNamespaceResolver

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

Zwraca

XPathNodeIterator

Element XPathNodeIterator wskazujący wybrany zestaw węzłów.

Wyjątki

Wyrażenie XPath zawiera błąd lub jego zwracany typ nie jest zestawem węzłów.

Wyrażenie XPath jest nieprawidłowe.

Przykłady

Poniższy przykład ilustruje wybieranie zestawu węzłów przy użyciu Select metody z obiektem XmlNamespaceManager określonym w celu rozpoznawania prefiksów przestrzeni nazw w wyrażeniu 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

W przykładzie contosoBooks.xml plik jest 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>  

Dotyczy