As you saw, XmlNode provides a way to navigate DOM trees with the help of its FirstChild, ChildNodes, LastChild, PreviousNode, NextSibling, and PreviousSibling methods.
Besides XmlNode, the XML .NET has two more classes, which help you navigate XML documents. These classes are XPathDocument and XPathNavigator. The System.Xml.XPath namespace defines both of these classes.
The XPath namespace contains classes to provide read-only, fast access to documents. Before using these classes, you must add a reference to the System.Xml.XPath namespace in your application.
XPathNodeIterator, XPathExpression, and XPathException are other classes defined in this namespace. The XPathNodeIterator class provides iteration capabilities to a node. XPathExpression provides selection criteria to select a set of nodes from a document based on those criteria, and the XPathException class is an exception class. TheXPathDocument class provides a fast cache for XML document processing using XSLT and XPath.
You use the XPathDocument constructor to create an instance of XmlPathDocument. It has many overloaded constructors. You can pass an XmlReader, TextReader, or even direct XML filenames.
The XPathNavigator class implements the functionality to navigate through a document. It has easy-to-use and self-explanatory methods. You create an XPathNavigator instance by calling XPathDocument's CreateNavigator method.
You can also create a XPathNavigator object by calling XmlDocument's CreateNavigator method. For example, the following code calls XmlDocument's CreateNavigator method to create a XPathNavigator object:
' Load books.xml document Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load("c:\\books.xml") ' Create XPathNavigator object by calling CreateNavigator of XmlDocument Dim nav As XPathNavigator = xmlDoc.CreateNavigator()
Note | Don't forget to add a reference to the System.Xml.XPath namespace to your project before using any of its classes. |
The XPathNavigator class contains methods and properties to move to the first, next, child, parent, and root nodes of the document.
Table 6-7 describes XPathNavigator's Move methods.
MEMBER | DESCRIPTION |
---|---|
MoveToAttribute | Moves to an attribute |
MoveToFirst | Moves to the first sibling of the current node |
MoveToFirstAttribute | Moves to the first attribute |
MoveToFirstChild | Moves to the first child of the current node |
MoveToFirstNamespace | Moves the XPathNavigator to first namespace node of the current element |
MoveToId | Moves to the node with specified ID |
MoveToNamespace | Moves to the specified namespace |
MoveToNext | Moves to the next node of the current node |
MoveToNextAttribute | Moves to the next attribute |
MoveToNextNamespace | Moves to the next namespace |
MoveToParent | Moves to the parent of the current node |
MoveToPrevious | Moves to the previous sibling of the current node |
MoveToRoot | Moves to the root node |
So, with the help of these methods, you can move through a document as a DOM tree. Listing 6-25 uses the MoveToRoot and MoveToFirstChild methods to move to the root node and first child of the root node. Once you have a root, you can display corresponding information such as the name, value, node type, and so on.
Listing 6-25: Moving to Root and First Child Nodes Using XPathNavigator
' Load books.xml document Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load("c:\\books.xml") ' Create XPathNavigator object by calling CreateNavigator of XmlDocument Dim nav As XPathNavigator = xmlDoc.CreateNavigator() ' Move to root node nav.MoveToRoot() Dim name As String = nav.Name Console.WriteLine("Root node info: ") Console.WriteLine("Base URI" + nav.BaseURI.ToString()) Console.WriteLine("Name: " + nav.Name.ToString()) Console.WriteLine("Node Type: " + nav.NodeType.ToString()) Console.WriteLine("Node Value: " + nav.Value.ToString()) If nav.HasChildren Then nav.MoveToFirstChild() End If
Now, using the MoveToNext and MoveToParent methods, you can move through the entire document. Listing 6-26 moves though an entire document and displays the data on the console. The GetNodeInfo method displays a node's information, and you call it recursively.
Listing 6-26: Reading a Document Using XPathNavigator
Imports System.Data Imports System.Data.SqlClient Imports System.Xml Imports System.Xml.XPath Module Module1 Sub Main() ' Load books.xml document Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load("c:\\books.xml") ' Create XPathNavigator object by calling CreateNavigator of XmlDocument Dim nav As XPathNavigator = xmlDoc.CreateNavigator() ' Moce to root node nav.MoveToRoot() Dim name As String = nav.Name Console.WriteLine("Root node info: ") Console.WriteLine("Base URI" + nav.BaseURI.ToString()) Console.WriteLine("Name: " + nav.Name.ToString()) Console.WriteLine("Node Type: " + nav.NodeType.ToString()) Console.WriteLine("Node Value: " + nav.Value.ToString()) If nav.HasChildren Then nav.MoveToFirstChild() GetNodeInfo(nav) End If End Sub ' GetNodeInfo method Public Sub GetNodeInfo(ByVal nav1 As XPathNavigator) Console.WriteLine("Name: " + nav1.Name.ToString()) Console.WriteLine("Node Type: " + nav1.NodeType.ToString()) Console.WriteLine("Node Value: " + nav1.Value.ToString()) ' If node has children, move to fist child. If nav1.HasChildren Then nav1.MoveToFirstChild() While nav1.MoveToNext() GetNodeInfo(nav1) nav1.MoveToParent() End While Else nav1.MoveToNext() GetNodeInfo(nav1) End If End Sub End Module
Select, SelectChildren, SelectAncestors, and SelectDescendents are other useful methods. Specifically, these methods are useful when you need to select a document's items based on an XPath expression. For example, you could use one when selecting nodes for the author tag only.
Now, say you want to search and display all <first-name> tag nodes in the books.xml document. In Listing 6-27, you use XPathNavigator's Select method to apply a criteria (all elements with the <first-name> tag) to read and display all the nodes.
Listing 6-27: Using XPathIterator and Select
' Load books.xml document Dim xmlDoc As XmlDocument = New XmlDocument() xmlDoc.Load("c:\\books.xml") ' Create XPathNavigator object by calling CreateNavigator of XmlDocument Dim nav As XPathNavigator = xmlDoc.CreateNavigator() ' Look for author's first name Console.WriteLine("Author First Name") Dim itrator As XPathNodeIterator = _ nav.Select("descendant::first-name") While itrator.MoveNext() Console.WriteLine(itrator.Current.Value.ToString()) End While