This documentation is archived and is not being maintained.

Select Nodes Using XPath Navigation

The DOM contains methods that allow you to use XPath navigation to query information in the DOM. You can use XPath to find a single, specific node, or to find all nodes that match some criteria. Retrieving a node or multiple nodes in the DOM without XPath requires a lot of navigation code. XPath, however, requires one line of code. For example, the following sample shows how to retrieve a single book node from an XmlDocument called doc.

Dim bookNode as XmlNode
bookNode = nav.SelectSingleNode("//book")
XmlNode bookNode = nav.SelectSingleNode("//book");

For finding a specific node, the SelectSingleNode method is used. If there are multiple nodes matching your search criteria, then the SelectSingleNode returns the first node that matches the search criteria, navigating from top of the tree down, in document order. To retrieve multiple nodes into a node set, the SelectNodes method is used.

The SelectNodes returns a set of nodes, which are put into an XmlNodeList object. The XmlNodelist has been enumerated to provide foreach statement capability. This gives the ability to loop through the nodes in the XmlNodeList.

The following sample example shows an XPath query returning all nodes that have a book author of Smith.

Dim root As XmlNode = doc.DocumentElement
nodeList = root.SelectNodes("descendant::book[author/last-name='Smith']")
XmlNode root = doc.DocumentElement;
nodeList = root.SelectNodes("descendant::book[author/last-name='Smith']");

Once a node list has been generated, the list can be looped over as shown in the following sample.

Dim book as XmlNode
For Each book in nodeList
   ' Do something.
foreach (XmlNode book in nodeList)
    // Do something.

The root variable was set to the document element. Setting the starting point for the XPath query sets the context node, which is the starting point where the XPath query starts from. The example above starts the XPath query at the context node of the DocumentElement, stored in the variable root. If you do not want to start at the document element, but want to start from the first child of the document element, you can code the Select statement shown in the following example.

doc.DocumentElement.FirstChild.Select(. . . )
this doc.DocumentElement.FirstChild.Select(. . .);

All XmlNodeList objects are synchronized with the underlying document. Therefore, if you iterate through the node list and modify the value of a node, that node is also updated in the document it came from.

Note   When the underlying document is modified, it is advisable to re-run the select. If the node modified is one that could cause the node to be added to the node list when it wasn't previously, or would now cause it to be removed from the node list, there is no guarantee that the node list is now accurate.

The following example modifies the price of a book in the node list, and at the same time, modifies the price of that node in the underlying document.

' Create the XmlDocument.
    Dim doc as XmlDocument = new XmlDocument()
    Dim book as XmlNode
    Dim nodeList as XmlNodeList 
    Dim root as XmlNode = doc.DocumentElement

    ' Change the price on the books.
    for each book in nodeList      

    Console.WriteLine("Display the modified XML document....")
XmlDocument doc = new XmlDocument();

XmlNodeList nodeList;
XmlNode root = doc.DocumentElement;
// Change the price on the books.
foreach (XmlNode book in nodeList)
Console.WriteLine("Display the modified XML document....");

Both the SelectNodes and SingleSelectNodes methods have an overloaded method that take a namespace manager parameter. The XPath query uses the set of namespaces in the namespace manager to match the namespaces for the nodes you are looking for. For more information on the namespace manager, see Namespaces in an XML Document.

See Also

XML Document Object Model (DOM)