How to query XML using XPath Expressions

xiaoxiao2021-03-06  40

How to query XML using XPath Expressions

This example explains how to use the W3C XML Path Language (XPath) expressions provided to the XPathnavigator class (XPathDocument. The XPathnavigator class is only used to read only the XPath query for the document, and for the Extensible Style Language Conversion (XSLT), the XSLTransform class is implemented by the XSLTransform class. For XSLT, you don't need to create XPathnavigator.

Note: XPath is a general query language specification for W3C to address certain parts of the XML document. XPath's .NET framework implements the WWW Federation (W3C) XML Path Language (XPath) 1.0 version specification.

To use XSLT to quickly and efficiently with XML documents, use the XPathDocument class. It can be considered that the XPathDocument class is similar to XML DOM, but the former is highly optimized for the XSLT processing and the XPath data model. However, different from the W3C XML DOM class is that the XPathDocument class does not maintain node identifiers, nor does all rules and verification checks required by the DOM class.

You can also perform XPath queries for the XMLDocument or XMLDATADOCUMENT class. If you use one of these two classes, you need to use the SelectNodes and Selectsinglenode methods of the XMLNode class.

VB QueryxmldocumentXPath.aspx

[

Run example] | [[[[

View Source Code]

This example loads an XPathDocument containing the sample file books.xml. This example is then passed to the XPathQuery function. The first XPath expression to be passed select all the price nodes of all book elements nodes starting from the root node. The second XPath expression gets the text of the ISBN property node that is associated with the last book element node starting from the root node.

To select a node that matches the given XPath expression, the XPathQuery function uses the XPathnavigator's SELECT method. The function then creates an XPathNodeEiterator and uses this iteration program to repeat a node and display the node on the screen.

Note: Because XPathNodeEiterator represents an XPath node set, XPathNodeIterator supports the operation on this node set.