Select XML Data Using XPathNavigator

The XPathNavigator class provides a set of methods used to select a set of nodes in an XPathDocument or XmlDocument object using an XPath expression. Once selected, you can iterate over the selected set of nodes.

XPathNavigator Selection Methods

The XPathNavigator class provides a set of methods used to select a set of nodes in an XPathDocument or XmlDocument object using an XPath expression. The XPathNavigator class also provides a set of optimized methods for selecting ancestor, child and descendant nodes faster than using an XPath expression. The selected set of nodes is returned in an XPathNodeIterator object or an XPathNavigator object in the case of a single selected node.

Selecting Nodes Using XPath Expressions

To select a set of nodes using an XPath expression, use one of the following selection methods.

When called, these methods return a set of nodes that you can navigate freely using an XPathNodeIterator object or an XPathNavigator object in the case of a single selected node.

Navigating with an XPathNodeIterator object does not affect the position of the XPathNavigator object used to create it. The XPathNavigator object returned from the SelectSingleNode methods is positioned on the single returned node and also does not affect the position of the XPathNavigator object used to create it.

The following example shows the creation of an XPathNavigator object from an XPathDocument object, the use of the Select method to select nodes in the XPathDocument object, and the use of the XPathNodeIterator object to iterate over the selected nodes.

Dim document As XPathDocument = New XPathDocument("books.xml")  
Dim navigator As XPathNavigator = document.CreateNavigator()  
Dim nodes As XPathNodeIterator = navigator.Select("/bookstore/book")  
  
While nodes.MoveNext()  
    Console.WriteLine(nodes.Current.Name)  
End While  
XPathDocument document = new XPathDocument("books.xml");  
XPathNavigator navigator = document.CreateNavigator();  
XPathNodeIterator nodes = navigator.Select("/bookstore/book");  
  
while(nodes.MoveNext())  
{  
    Console.WriteLine(nodes.Current.Name);  
}  

The example takes the books.xml file as 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>

Optimized Selection Methods

The SelectChildren, SelectAncestors, and SelectDescendants methods of the XPathNavigator class represent XPath expressions commonly used to retrieve child, descendant, and ancestor nodes. These methods are optimized for performance and are faster than their corresponding XPath expressions. The SelectChildren, SelectAncestors, and SelectDescendants methods selects ancestor, child, and descendant nodes based on an XPathNodeType value or the local name and namespace URI of the nodes to select. The selected ancestor, child, and descendant nodes are returned in an XPathNodeIterator object.

See also