create a DocumentNavigator as a view onto the data and display the XML by moving through the document. The <b>DocumentNavigator</b> class provides an implementation
of the XmlNavigator for the XmlDocument class and the <b>DataDocumentNavigator</b> class provides an implementation of the XmlNavigator for the XmlDataDocument class.
The sample code below shows the creation of these classes.
<div class="code">
<xmp>
// Create a DocumentNavigator
XmlDocument xmldocument = new XmlDocument();
DocumentNavigator navigator = new DocumentNavigator(xmldocument);
// Create a DataDocumentNavigator
XmlDataDocument xmldatadocument = new XmlDataDocument();
DataDocumentNavigator datanavigator = new DataDocumentNavigator(xmldatadocument);
</xmp></div>
<div style="width:660">
For the XmlNavigator, the properties and methods operate in relation to the current node. The <b>MoveToNext</b> method moves to the next sibling of the
current node. <b>The MoveToChild(i)</b> method moves to the specified child of the current node (where 0 is the first child).
The <b>MoveToParent</b> method moves to the parent node of the current node. When positioned on an attribute you must use the <b>MoveToElement</b>
method to move to the element that owns the attribute node. This is how you get back to the element after navigating through its attributes.
The <b>HasChildren</b> property indicates whether the current node has child nodes and the <b>HasAttributes</b> property indicates whether
the current node has attribute nodes. Lastly the <b>MoveToDocument</b> method sets the navigator to the document node that contains the whole tree of nodes.
These methods enable us to navigate the XML document in a recursive fashion as illustrated in the code below.</div>
<div class="code">
<xmp>
navigator.MoveToDocument(); // Initialize the Navigator to start at the root
If the structure of the XML is known, then it can be navigated by explicitly moving through the document from node to node. The code below finds the price of the
first book in the sample file books.xml.</div>
<div class="code"><xmp>
Console.WriteLine ("Find the price of the first book by navigating nodes ...");
The output from executing this code is shown below.</div>
<div class="code"><xmp>
Find the price of the first book by navigating nodes ...
<#document>
<xml>
<#comment>
<bookstore>
<book>
<title>
<author>
<price>
<#text>8.99
</xmp></div>
<h4>Selecting a Set of Nodes</h4>
<div style="width:660">
One of the most powerful features of the XmlNavigator is its ability to create a set of selected nodes with an XPath expression using the <b>Select</b>
method. XPath is a language that provides filtering and addressing of data in an XML document and is a more intuative approach to retrieving a set of nodes.
See <a href="QueryXmlDocumentXPath.aspx">How Do I...Query XML with an XPath expression?</a>
The XmlNavigator can apply a query and generate a set of nodes that correspond to the selected query set and these can be navigated independently. The methods
<b>MoveToFirstSelected</b> and <b>MoveToNextSelected</b> allow navigation over this selected tree. Note that from any of the selected nodes it is possible
to navigate to the next sibling of the selected node using <b>MoveNext</b> method or any of the other navigation methods. I.e. You can move between the
selected nodes and the whole tree of nodes depending upon the navigation methods used. The code example below selects and displays all the book titles from
The <b>InnerText</b> property on a node, either gets the concatenation for the text values of all it's children as a string, or replaces all the child nodes
with a single text node that has a value of the supplied string. It is used here since the selected elements only have a single child text node for the
title of the book. The output from executing this code is shown.</div>
<div class="code">
<xmp>
Select the book titles ...
<title>The Autobiography of Benjamin Franklin
<title>The Confidence Man
<title>The Gorgias
</xmp></div>
<H4>Summary</H4>
<OL>
<LI>The XmlNavigator provides a cursor style model to navigate an XML in-memory tree.
<LI>The DocumentNavigator and DataDocumentNavigator classes are implementations of the XmlNavigator.
<LI>The MoveTo methods allow you to perform random access operations on the XML document, and to directly walk the XML nodes.
<LI>The Select method, using XPath expressions, is a powerful method of creating a set of selected nodes.
<LI>The MoveToNextSelected method moves across the tree of selected nodes.