XPath and XSL conversion-use xpathnavigator to locate

Source: Internet
Author: User
How to Use xpathnavigator to browse XML

This example illustrates how to use the xpathnavigator created from xpathdocument to browse XML documents. It also describes how to use xpathnodeiterator. The xpathnavigator class provides a model of the cursor style to browse XML documents. This topic also introduces XML Path Language (XPath) expressions, which provides more details about how to use XPath expressions to query XML.

 

VB navigatexmldocument. aspx

[Running example] | [View Source code]

In this example, an xpathdocument (containing XML data in books. XML) is loaded, an xpathnavigator is created as a view to view the data, and the XML is displayed by moving in the document. The xpathnavigator class provides XPath APIs for data, such as xpathdocument. ExampleCodeShows the process of creating these classes.


Xpathdocument myxpathdocument = new xpathdocument (ARGs);... // create an xpathnavigator myxpathnavigator = myxpathdocument. createnavigator ();
Dim myxpathdocument as xpathdocument = new xpathdocument (ARGs)... 'create an xpathnavigator dim myxpathnavigator as xpathnavigator = myxpathdocument. createnavigator ()
C # VB  

Xpathnavigator allows you to move data between attribute nodes and namespace nodes in the XML document.

The method and attribute of xpathnavigator are compared to the current node:

    • Move the movetofirstchild Method to the first subnode of the current node.
    • Move the movetoparent Method to the parent node of the current node. When you locate an attribute, you must use the movetoparent method to move it to the element that owns the attribute node. This is how to return to the element after you use movetofirstattribute and movetonextattribute to browse the attributes of the element.
    • The haschildren attribute indicates whether the current node has subnodes.
    • The hasattributes attribute indicates whether the current node has an attribute node.
    • The movetoroot method locates the navigator on the document node that contains the entire node tree.

These methods allow you to recursively browse XML documents, as shown in the following code.

Myxpathnavigator. movetoroot (); // initialize the myxpathnavigator to start at the root displaytree (myxpathnavigator); // display all the nodes... // walks the xpathnavigator tree recursively public void displaytree (xpathnavigator myxpathnavigator) {If (myxpathnavigator. haschildren) {myxpathnavigator. movetofirstchild (); format (myxpathnavigator); displaytree (myxpathnavigator); myxpathnavigator. Movetoparent ();} while (myxpathnavigator. movetonext () {format (myxpathnavigator); displaytree (myxpathnavigator) ;}// format the output private void format (xpathnavigator myxpathnavigator) {If (! Myxpathnavigator. haschildren) {If (myxpathnavigator. nodetype = xpathnodetype. Text) console. writeline (myxpathnavigator. Value); else if (myxpathnavigator. Name! = String. empty) console. writeline ("<" + myxpathnavigator. name + ">"); else console. writeline ();} else {console. writeline ("<" + myxpathnavigator. name + ">"); // show the attributes if there are any if (myxpathnavigator. hasattributes) {console. writeline ("attributes of <" + myxpathnavigator. name + ">"); While (myxpathnavigator. movetonextattribute () console. write ("<" + myxpathnavigator. name + ">" + myxpathnavigator. value + ""); // return to the 'parent' node of the attributes myxpathnavigator. movetoparent ();}}}
Myxpathnavigator. movetoroot () 'initialise the myxpathnavigator to start at the root displaytree (myxpathnavigator) 'display all the nodes... 'walks the xpathnavigator tree recursively public sub displaytree (myxpathnavigator as xpathnavigator) if (myxpathnavigator. haschildren) myxpathnavigator. movetofirstchild () format (myxpathnavigator) displaytree (myxpathnavigator) myxpathnavigator. movetoparent () end if while (myxpathnavigator. movetonext () format (myxpathnavigator) displaytree (myxpathnavigator) end while end sub 'format the output private sub format (myxpathnavigator as xpathnavigator) if not (myxpathnavigator. haschildren) if (myxpathnavigator. nodetype = xpathnodetype. text) console. writeline (myxpathnavigator. value) elseif (myxpathnavigator. name <> string. empty) console. writeline ("<" & myxpathnavigator. name & ">") else console. writeline () end if else console. writeline ("<" & myxpathnavigator. name & ">") 'Show the attributes if there are any if (myxpathnavigator. hasattributes) console. writeline ("attributes of <" & myxpathnavigator. name & ">") while (myxpathnavigator. movetonextattribute () console. write ("<" + myxpathnavigator. name + ">" + myxpathnavigator. value + "") end while 'return to the 'parent' node of the attributes myxpathnavigator. movetoparent () end if end sub
C # VB  

If you know the structure of the XML document, you can browse the document by explicitly moving from one node to another. The following code finds the price of the first book node in the example file books. xml.

// Find the price of the first book. start at the root node console. writeline (); console. writeline ("find the price of the first book by navigating nodes... "); myxpathnavigator. movetoroot (); displaynode (true, myxpathnavigator); // root node displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator );//? XML version = '1. 0 '? Node displaynode (myxpathnavigator. movetonext (), myxpathnavigator );//! -- This file... node displaynode (myxpathnavigator. movetonext (), myxpathnavigator); // bookstore element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator); // book element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator); // Title element displaynode (myxpathnavigator. movetonext (), myxpathnavigator); // author element displaynode (myxpathnavigator. movetonext (), Myxpathnavigator); // price element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator); // value of price element... private void displaynode (Boolean success, xpathnavigator myxpathnavigator) {If (success & (myxpathnavigator. nodetype = xpathnodetype. text) console. writeline (myxpathnavigator. value); else if (success & (myxpathnavigator. name! = String. Empty) console. writeline ("<" + myxpathnavigator. Name + ">"); else console. writeline ();}
'Find the price of the first book. start at the root node console. writeline () console. writeline ("find the price of the first book by navigating nodes... ") myxpathnavigator. movetoroot () displaynode (true, myxpathnavigator) 'root node displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator )'? XML version = '1. 0 '? Node displaynode (myxpathnavigator. movetonext (), myxpathnavigator )'! -- This file... node displaynode (myxpathnavigator. movetonext (), myxpathnavigator) 'bookstore element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator) 'book element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator) 'title element displaynode (myxpathnavigator. movetonext (), myxpathnavigator) 'author element displaynode (myxpathnavigator. movetonext (), myxpathnavigator) 'price element displaynode (myxpathnavigator. movetofirstchild (), myxpathnavigator) 'value of price element... private sub displaynode (success as Boolean, myxpathnavigator as xpathnavigator) if (success and (myxpathnavigator. nodetype = xpathnodetype. text) console. writeline (myxpathnavigator. value) else if (success and (myxpathnavigator. name <> string. empty) console. writeline ("<" & myxpathnavigator. name & ">") else console. writeline () end if end sub
C # VB  

The following code illustrates one of the most powerful functions of xpathnavigator-you can use the select method to create a group of selected nodes from an XPATH expression. XPath is a language that provides filtering and addressing for data in XML documents, and is another method for retrieving node sets. Xpathnavigator can apply an XPATH expression and generate a node set corresponding to the selected expression. Then, you can browse the node set separately because these nodes are returned to the xpathnodeiterator. The movenext method allows read-only browsing of the selected node tree. The following code example selects and displays the title subnode of each book node in the example file books. xml.

Xpathnodeiterator myxpathnodeiterator = myxpathnavigator. select ("Descendant: Book/Title"); While (myxpathnodeiterator. movenext () {console. writeline ("<" + myxpathnodeiterator. current. name + ">" + myxpathnodeiterator. current. value );};
Dim myxpathnodeiterator as xpathnodeiterator = myxpathnavigator. select ("Descendant: Book/Title") while (myxpathnodeiterator. movenext () console. writeline ("<" & myxpathnodeiterator. current. name & ">" & myxpathnodeiterator. current. value) end while
C # VB  
Summary
    1. the xpathnavigator class provides a cursor-style model to browse XML documents in memory.
    2. the xpathnavigator class provides XPath APIs for data, such as xpathdocument.
    3. xpathnavigator allows you to move data between attribute nodes and namespace nodes in the XML document.

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.