home *** CD-ROM | disk | FTP | other *** search
/ io Programmo 40 / IOPROG_40.ISO / SOFT / NETFrameworkSDK.exe / comsdk.cab / samples.exe / QuickStart / howto / doc / XML / NavigateXmlDocument.aspx < prev    next >
Encoding:
Text File  |  2000-06-10  |  9.4 KB  |  271 lines

  1. \<%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" -->
  4.  
  5. <h4>How Do I...Navigate XML with the XmlNavigator?</h4>
  6.  
  7. <div class="indent" style="width:660">
  8. This sample illustrates how to navigate XML documents with an XmlNavigator using the DocumentNavigator and DataDocumentNavigator.
  9. The XmlNavigator class provides a cursor style model to navigate an XML in-memory tree. The XmlNavigator also provides XPath support and is the
  10. input to XslTransforms. XPath expressions are introduced here with more detail added in the
  11. <a href="QueryXmlDocumentXPath.aspx">How Do I...Query XML with an XPath expression?</a> topic.
  12. </div>
  13.  
  14. <h4>Navigating XML with the XmlNavigator</h4> 
  15. <Acme:SourceRef
  16. ViewSource="/quickstart/howto/samples/Xml/NavigateXmlDocument/NavigateXmlDocument.src"
  17. RunSample="/quickstart/howto/samples/Xml/NavigateXmlDocument/NavigateXmlDocument.aspx" 
  18. Icon = "/quickstart/images/genicon.gif"
  19. Caption="NavigateXmlDocument.aspx"
  20. runat="server" />
  21.  
  22. <br clear="left"><br>
  23. <div class="indent" style="width:660">
  24. In this sample we are going to load an XmlDocument with XML data using the sample file
  25. <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/NavigateXmlDocument/&file=books.xml">books.xml</a>,
  26. 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
  27. of the XmlNavigator for the XmlDocument class and the <b>DataDocumentNavigator</b> class provides an implementation of the XmlNavigator for the XmlDataDocument class.
  28. The sample code below shows the creation of these classes.
  29.  
  30. <div class="code">
  31. <xmp>
  32. // Create a DocumentNavigator
  33. XmlDocument xmldocument = new XmlDocument();
  34. DocumentNavigator navigator = new DocumentNavigator(xmldocument);
  35.  
  36. // Create a DataDocumentNavigator
  37. XmlDataDocument xmldatadocument = new XmlDataDocument();
  38. DataDocumentNavigator datanavigator = new DataDocumentNavigator(xmldatadocument);
  39. </xmp></div>
  40.  
  41. <div style="width:660">
  42. 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 
  43. current node. <b>The MoveToChild(i)</b> method moves to the specified child of the current node (where 0 is the first child).
  44. 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>
  45. 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.
  46. The <b>HasChildren</b> property indicates whether the current node has child nodes and the <b>HasAttributes</b> property indicates whether
  47. 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.
  48. These methods enable us to navigate the XML document in a recursive fashion as illustrated in the code below.</div>
  49.  
  50. <div class="code">
  51. <xmp>
  52. navigator.MoveToDocument(); // Initialize the Navigator to start at the root
  53. DisplayTree(navigator); // Display all the nodes
  54.  
  55. // Walks the XmlNavigator tree recursively 
  56. public void DisplayTree (XmlNavigator navigator)
  57. {
  58.     if (navigator.HasChildren)
  59.     {
  60.         navigator.MoveToChild(0);
  61.         Format (navigator);ww
  62.         DisplayTree (navigator); 
  63.         navigator.MoveToParent();
  64.     }
  65.     while (navigator.MoveToNext())
  66.     {
  67.         Format (navigator);
  68.         DisplayTree (navigator); 
  69.     }
  70. }
  71.  
  72. // Format the output
  73. private void Format (XmlNavigator navigator)
  74. {
  75.     if (!navigator.HasChildren)
  76.     {
  77.         Console.Write("<" + navigator.Name + ">" + navigator.Value + "\r\n");
  78.     }
  79.     else
  80.     {
  81.         Console.Write("<" + navigator.Name + ">");
  82.  
  83.         if (navigator.HasAttributes)
  84.             Console.Write("\r\nAttributes of <" + navigator.Name + ">\r\n");
  85.  
  86.         for (int i = 0; i < navigator.AttributeCount; i++)
  87.         {
  88.             navigator.PushPosition();
  89.             navigator.MoveToAttribute(i);
  90.             Console.Write("<" + navigator.Name + "> " + navigator.Value + " ");
  91.             navigator.PopPosition();
  92.         }
  93.         Console.WriteLine();
  94.     }
  95. }
  96. </xmp></div>
  97.  
  98. <div style="width:660">
  99. The output from executing this code is shown below.</div>
  100.  
  101. <div class="code"><xmp>
  102. Display contents of the file books.xml
  103.  
  104. <xml>version='1.0'
  105. <#comment> This file represents a fragment of a book store inventory database
  106. <bookstore>
  107. <book>
  108. Attributes of <book>
  109. <genre> autobiography <publicationdate> 1981 <ISBN> 1-861003-11-0
  110. <title>
  111. <#text>The Autobiography of Benjamin Franklin
  112. <author>
  113. <first-name>
  114. <#text>Benjamin
  115. <last-name>
  116. <#text>Franklin
  117. <price>
  118. <#text>8.99
  119. <book>
  120. Attributes of <book>
  121. <genre> novel <publicationdate> 1967 <ISBN> 0-201-63361-2
  122. <title>
  123. <#text>The Confidence Man
  124. <author>
  125. <first-name>
  126. <#text>Herman
  127. <last-name>
  128. <#text>Melville
  129. <price>
  130. <#text>11.99
  131. <book>
  132. Attributes of <book>
  133. <genre> philosophy <publicationdate> 1991 <ISBN> 1-861001-57-6
  134. <title>
  135. <#text>The Gorgias
  136. <author>
  137. <name>
  138. <#text>Plato
  139. <price>
  140. <#text>9.99
  141. </xmp></div>
  142.         
  143. <h4>Explicit Moves</h4> 
  144.  
  145. <div style="width:660">
  146. 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
  147. first book in the sample file books.xml.</div> 
  148.  
  149. <div class="code"><xmp>
  150. Console.WriteLine ("Find the price of the first book by navigating nodes ...");
  151. navigator.MoveToDocument();
  152. DisplayNode (true, navigator); // root node
  153. DisplayNode (navigator.MoveToFirstChild(), navigator); // ?xml version='1.0'? node
  154. DisplayNode (navigator.MoveToNext(), navigator); //!-- This file ... node
  155. DisplayNode (navigator.MoveToNext(), navigator); // bookstore element
  156. DisplayNode (navigator.MoveToFirstChild(), navigator); // book element
  157. DisplayNode (navigator.MoveToFirstChild(), navigator); // title element
  158. DisplayNode (navigator.MoveToNext(), navigator);// author element
  159. DisplayNode (navigator.MoveToNext(), navigator); // price Element
  160. DisplayNode (navigator.MoveToFirstChild(), navigator); // value of price element
  161.  
  162. private void DisplayNode(Boolean success, XmlNavigator navigator)
  163. {
  164.     if (success && (navigator.NodeType == XmlNodeType.Text))
  165.         Console.Write("<" + navigator.Name + ">" + navigator.Value + "\r\n");
  166.     else if (success)
  167.         Console.Write("<" + navigator.Name + ">\r\n");
  168.     else
  169.         Console.WriteLine();
  170. }
  171. </xmp></div>
  172.  
  173. <div style="width:660">
  174. The output from executing this code is shown below.</div>
  175.  
  176. <div class="code"><xmp>
  177. Find the price of the first book by navigating nodes ...
  178. <#document>
  179. <xml>
  180. <#comment>
  181. <bookstore>
  182. <book>
  183. <title>
  184. <author>
  185. <price>
  186. <#text>8.99
  187. </xmp></div>
  188.  
  189. <h4>Selecting a Set of Nodes</h4> 
  190.  
  191. <div style="width:660">
  192. 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>
  193. 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.
  194. See <a href="QueryXmlDocumentXPath.aspx">How Do I...Query XML with an XPath expression?</a>
  195. 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
  196. <b>MoveToFirstSelected</b> and <b>MoveToNextSelected</b> allow navigation over this selected tree. Note that from any of the selected nodes it is possible
  197. 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 
  198. 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 
  199. the sample file books.xml.</div>
  200.  
  201. <div class="code">
  202. <xmp>
  203. navigator.MoveToDocument();
  204. navigator.Select("descendant::book/title");
  205.  
  206. while(navigator.MoveToNextSelected())
  207. {
  208.     Console.Write("<" + navigator.Name + ">" + navigator.InnerText + "\r\n");
  209. };
  210. </xmp></div>
  211.  
  212. <div style="width:660">
  213. 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
  214. 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 
  215. title of the book. The output from executing this code is shown.</div>
  216.  
  217. <div class="code">
  218. <xmp>
  219. Select the book titles ...
  220. <title>The Autobiography of Benjamin Franklin
  221. <title>The Confidence Man
  222. <title>The Gorgias
  223. </xmp></div>
  224.  
  225. <H4>Summary</H4>
  226. <OL>
  227. <LI>The XmlNavigator provides a cursor style model to navigate an XML in-memory tree. 
  228. <LI>The DocumentNavigator and DataDocumentNavigator classes are implementations of the XmlNavigator.
  229. <LI>The MoveTo methods allow you to perform random access operations on the XML document, and to directly walk the XML nodes.
  230. <LI>The Select method, using XPath expressions, is a powerful method of creating a set of selected nodes.
  231. <LI>The MoveToNextSelected method moves across the tree of selected nodes.
  232. </LI></OL>
  233.  
  234. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  235.  
  236. </xml>
  237.  
  238.  
  239.  
  240.  
  241.  
  242.  
  243.  
  244.  
  245.  
  246.  
  247.  
  248.  
  249.  
  250.  
  251.  
  252.  
  253.  
  254.  
  255.  
  256.  
  257.  
  258.  
  259.  
  260.  
  261.  
  262.  
  263.  
  264.  
  265.  
  266.  
  267.  
  268.  
  269.  
  270.  
  271.