Share via


XPath Select and XPathNavigator

A set of nodes can be selected from any store that implements the IXPathNavigable class. Classes that already implement IXPathNavigable include XPathDocument, XmlDocument, XmlDataDocument, and XmlNode.

The following example shows the creation of an XPathNavigator on an XPathDocument, the use of the Select method to select nodes, and the use of the XPathNodeIterator to move over the nodes.

Dim Doc As XPathDocument = New XPathDocument(FileName)
Dim Nav As XPathNavigator = Doc.CreateNavigator()
Dim Iterator as XPathNodeIterator = Nav.Select("/bookstore/book")
While Iterator.MoveNext()
....Console.WriteLine(Iterator.Current.Name)
End While
[C#]
XPathDocument Doc = new XPathDocument(FileName);
XPathNavigator nav = Doc.CreateNavigator();
XPathNodeIterator Iterator = nav.Select("/bookstore/book");
while (Iterator.MoveNext())
{
    Console.WriteLine(Iterator.Current.Name);
}

To iterate over the set of selected nodes, call methods that return an XPathNodeIterator. The various methods of selecting nodes that return an XPathNodeIterator are as follows:

  • Select
  • SelectChildren
  • SelectAncestors
  • SelectDescendents
  • Evaluate

When called, these methods return a set of nodes that you can navigate freely around using an XPathNodeIterator. Navigating with an XPathNodeIterator does not affect the position of the XPathNavigator that was used to create the XPathNodeIterator. The node set returned from the Select and Evaluate methods noted above have the following characteristics:

  • The node set is a virtual tree of nodes, in document order.
  • Attribute nodes are not included as part of the tree navigation methods.
  • Namespace nodes are not included as part of the tree navigation methods.

See Also

XPathNavigator in the .NET Framework | XPath Evaluate and XPathNavigator | XPath Matches and XPathNavigator