XPathNavigator.Select Método

Definição

Seleciona um nó definido usando a expressão XPath especificada.Selects a node set, using the specified XPath expression.

Sobrecargas

Select(String)

Seleciona um nó definido usando a expressão XPath especificada.Selects a node set, using the specified XPath expression.

Select(XPathExpression)

Seleciona um nó definido usando o XPathExpression especificado.Selects a node set using the specified XPathExpression.

Select(String, IXmlNamespaceResolver)

Seleciona um nó definido usando a expressão XPath especificada com o objeto IXmlNamespaceResolver especificado para resolver os prefixos de namespace.Selects a node set using the specified XPath expression with the IXmlNamespaceResolver object specified to resolve namespace prefixes.

Select(String)

Seleciona um nó definido usando a expressão XPath especificada.Selects a node set, using the specified XPath expression.

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

Parâmetros

xpath
String

Um String que representa uma expressão XPath.A String representing an XPath expression.

Retornos

XPathNodeIterator

Um XPathNodeIterator que aponta para o conjunto de nós selecionado.An XPathNodeIterator pointing to the selected node set.

Exceções

A expressão XPath contém um erro ou seu tipo de retorno não é um conjunto de nós.The XPath expression contains an error or its return type is not a node set.

A expressão XPath não é válida.The XPath expression is not valid.

Exemplos

O exemplo a seguir usa o Select método para selecionar um conjunto de nós.The following example uses the Select method to select a node set.

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

O exemplo usa o arquivo books.xml como entrada.The example takes the books.xml file as an input.

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

Comentários

O contexto para a seleção é a posição do XPathNavigator quando esse método é chamado.The context for the selection is the position of the XPathNavigator when this method is called. Depois de chamar esse método, o XPathNodeIterator retornado representa o conjunto de nós selecionados.After calling this method, the XPathNodeIterator returned represents the set of selected nodes. Use MoveNext o método do XPathNodeIterator para iterar sobre o conjunto de nós selecionado.Use MoveNext method of the XPathNodeIterator to iterate over the selected node set.

O código C# a seguir itera sobre o conjunto selecionado de nós.The following C# code iterates over the selected set of nodes.

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

Veja a seguir as observações importantes a serem consideradas ao usar o Select método.The following are important notes to consider when using the Select method.

Confira também

Aplica-se a

Select(XPathExpression)

Seleciona um nó definido usando o XPathExpression especificado.Selects a node set using the specified 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

Parâmetros

expr
XPathExpression

Um objeto XPathExpression que contém a consulta XPath compilada.An XPathExpression object containing the compiled XPath query.

Retornos

XPathNodeIterator

Um XPathNodeIterator que aponta para o conjunto de nó selecionado.An XPathNodeIterator that points to the selected node set.

Exceções

A expressão XPath contém um erro ou seu tipo de retorno não é um conjunto de nós.The XPath expression contains an error or its return type is not a node set.

A expressão XPath não é válida.The XPath expression is not valid.

Exemplos

O exemplo a seguir usa o Select método para selecionar um conjunto de nós.The following example uses the Select method to select a node set.

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

O exemplo usa o arquivo books.xml como entrada.The example takes the books.xml file as an input.

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

Comentários

O contexto para a seleção é a posição do XPathNavigator quando você chamou esse método.The context for the selection is the position of the XPathNavigator when you called this method. Depois de chamar esse método, o XPathNodeIterator retornado representa o conjunto de nós selecionados.After you call this method, the XPathNodeIterator returned represents the set of selected nodes. Use MoveNext no XPathNodeIterator para iterar sobre o conjunto de nós selecionado.Use MoveNext on the XPathNodeIterator to iterate over the selected node set.

O código C# a seguir itera sobre o conjunto selecionado de nós.The following C# code iterates over the selected set of nodes.

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

Veja a seguir as observações importantes a serem consideradas ao usar o Select método.The following are important notes to consider when using the Select method.

Por exemplo, suponha que o documento contenha os seguintes nós XML.For example, suppose the document contains the following XML nodes.

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

Nesse caso, o código C# a seguir seleciona o bk:ISBN nó.In this case, the following C# code selects the bk:ISBN node.

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

Observação

Se o XPathExpression não incluir um prefixo, supõe-se que o URI do namespace é o namespace vazio.If the XPathExpression does not include a prefix, it is assumed that the namespace URI is the empty namespace. Se o XML incluir um namespace padrão, você ainda deverá usar o SetContext método e fornecer um XmlNamespaceManager que contenha um prefixo e um URI de namespace para manipular o namespace padrão.If your XML includes a default namespace, you must still use the SetContext method and provide an XmlNamespaceManager that contains a prefix and namespace URI to handle the default namespace.

Por exemplo, suponha que você tenha o seguinte XML.For example, suppose you have the following XML.

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

Nesse caso, o seguinte código C# seleciona todos os nós de livros:In this case, the following C# code selects all book nodes:

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

Esse método não tem efeito sobre o estado do XPathNavigator .This method has no effect on the state of the XPathNavigator.

Confira também

Aplica-se a

Select(String, IXmlNamespaceResolver)

Seleciona um nó definido usando a expressão XPath especificada com o objeto IXmlNamespaceResolver especificado para resolver os prefixos de namespace.Selects a node set using the specified XPath expression with the IXmlNamespaceResolver object specified to resolve namespace prefixes.

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

Parâmetros

xpath
String

Um String que representa uma expressão XPath.A String representing an XPath expression.

resolver
IXmlNamespaceResolver

O objeto IXmlNamespaceResolver usado para resolver prefixos de namespace.The IXmlNamespaceResolver object used to resolve namespace prefixes.

Retornos

XPathNodeIterator

Um XPathNodeIterator que aponta para o conjunto de nó selecionado.An XPathNodeIterator that points to the selected node set.

Exceções

A expressão XPath contém um erro ou seu tipo de retorno não é um conjunto de nós.The XPath expression contains an error or its return type is not a node set.

A expressão XPath não é válida.The XPath expression is not valid.

Exemplos

O exemplo a seguir ilustra a seleção de um conjunto de nós usando o Select método com o XmlNamespaceManager objeto especificado para resolver prefixos de namespace na expressão XPath.The following example illustrates selecting a node set using the Select method with the XmlNamespaceManager object specified to resolve namespace prefixes in the XPath expression.

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

O exemplo usa o arquivo contosoBooks.xml como entrada.The example takes the contosoBooks.xml file as an input.

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

Aplica-se a