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

  1. <%@ Register TagPrefix="Acme" TagName="SourceRef" Src="/quickstart/util/SrcRef.aspx"%>
  2.  
  3. <!-- #include virtual="/quickstart/howto/doc/xml/header.inc" --><h4>How Do I...Query XML with an XPath expression?</h4>
  4.  
  5. <div class="indent" style="width:660">
  6. This sample illustrates how to query an XmlDataDocument or an XmlDocument with an XPath expression using the XmlNavigator class.
  7. This sample follows on from the <a href="NavigateXmlDocument.aspx">How Do I...Navigate XML with the XmlNavigator?</a> topic.
  8. The XPath queries conform to the <A href="http://www.w3.org/TR/xpath">W3C XML Path Language (XPath)</A> specification.
  9. </div>
  10.  
  11. <h4>Querying XML with an XPath expression</h4> 
  12. <Acme:SourceRef
  13. ViewSource="/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/QueryXmlDocumentXPath.src"
  14. RunSample="/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/QueryXmlDocumentXPath.aspx" 
  15. Icon = "/quickstart/images/genicon.gif"
  16. Caption="QueryXmlDocumentXPath.aspx"
  17. runat="server" />
  18.  
  19. <br clear="left"><br>
  20. <div class="indent" style="width:660">
  21. XPath is the W3C's general query language specification for addressing parts of an XML document. In this sample we are going to load an XmlDocument with the sample
  22. file <a target="_blank" href="/quickstart/util/srcctrlwin.aspx?path=/quickstart/howto/samples/Xml/QueryXmlDocumentXPath/&file=books.xml">books.xml</a>,
  23. perform an XPath query on XML and navigate over the selected nodes using an XmlNavigator. The sample code below illustrates the <b>Select</b> method that
  24. applies an XPath expression.<div>
  25.  
  26. <div class="code">
  27. <xmp>
  28. XmlDocument xmldocument = new XmlDocument();
  29. xmldocument.Load (m_Document);
  30. DocumentNavigator navigator = new DocumentNavigator(xmldocument);
  31.  
  32. // Get all the book prices
  33. XPathQuery(navigator, "descendant::book/price");
  34. // Get the ISBN of the last book
  35. XPathQuery(navigator, "//book[last()]/@ISBN/text()");
  36.  
  37. private void XPathQuery(XmlNavigator navigator, String xpathexpr )
  38. {
  39.     try
  40.     {
  41.         // Save context node position
  42.         navigator.PushPosition();
  43.         Console.WriteLine("XPath query: " + xpathexpr);
  44.  
  45.         navigator.Select (xpathexpr);
  46.  
  47.         FormatXml(navigator);
  48.  
  49.         // Restore context node position
  50.         navigator.PopPosition();        
  51.     }
  52.     catch (Exception e)
  53.     {
  54.         Console.WriteLine ("Exception: {0}", e.ToString());
  55.     }
  56. }
  57. </xmp></div>
  58. <div class="indent" style="width:660">
  59. The first query selects all the 'price' nodes for all the books starting from the root node. The second query gets the 'ISBN' attribute text
  60. for the last book element node starting from the root node.</div> 
  61.  
  62. <br clear="left"><div class="indent" style="width:660">
  63. The <b>PushPosition</b> method remembers the current position of the XmlNavigator on a stack. The selection state however, is not remembered. You can later
  64. return to this position by calling the <b>PopPosition</b> method. This enables you to move through the document using one select query and then return to a
  65. known position, to perform another query. In other words, PushPosition remembers the node you were on when called, so that when you call PopPosition, you can
  66. be taken back to that node. The output from these queries is shown.</div> 
  67.  
  68. <div class="code">
  69. <xmp>
  70. XPath query: descendant::book/price
  71. Element<price>8.99
  72. Element<price>11.99
  73. Element<price>9.99
  74.  
  75. XPath query: //book[last()]/@ISBN/text()
  76. Text<#text>1-861001-57-6
  77. </xmp>
  78. </div>
  79.  
  80. <H4>Summary</H4>
  81. <OL>
  82. <LI>XPath is the W3C's general query language specification for addressing parts of an XML document and is a powerful XML query language.
  83. <LI>The Select method applies the XPath query to the XML document.
  84. <LI>The MoveToNextSelected method moves between the nodes returned from the XPath query.
  85. <LI>The PushPosition method remembers the current position of the XmlNavigator on a stack. The selection state however, is not remembered. You can later
  86. return to this position by calling the PopPosition method.
  87. </LI></OL>
  88.  
  89. <!-- #include virtual="/quickstart/howto/include/footer.inc" -->
  90.  
  91.  
  92.  
  93.  
  94.  
  95.  
  96.  
  97.  
  98.  
  99.  
  100.  
  101.  
  102.  
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110.  
  111.  
  112.