Simplifying XML navigation with vb.net and XPath

Source: Internet
Author: User
Tags object end expression include net xpath access advantage
xml| Navigation

Using vb.net for XML navigation

The XML document may contain 1~1000 or more elements. You may need to access all the data contained in the XML document, or access a selected subset of the data. XPath simplifies this task by providing syntax for locating and selecting a subset of XML. NET Framework uses the XPathNavigator class to execute XPath commands, but to take advantage of it, you must have an XML document available.

The. NET XPathDocument class can be used to process the creation or retrieval of an XML document. This class provides a number of methods and attributes, but our focus is on the createnavigator approach. This method creates a XPathNavigator object that corresponds to an XML document. In the example of this article, you will use the following basic XML document:

<?xml version= "1.0" encoding= "Utf-8"
<sites>
<website>
<name type= "Application Development" >BUILDER.COM</NAME>
<link>www.builder.com</link>
</website>
<website>
<name type= "Information Technology" >TechRepublic</name>
<link>www.techrepublic.com</link>
</website>
<website>
<name type= " Technology News ">news.com</name>
<link>www.news.com</link>
</website>
</ Sites>

assumes that the above XML is stored in a local file, such as C:\builder.xml, you can assign a XPathNavigator object to it using the following vb.net code:

Dim Xpathdoc as XPathDocument
Dim Xmlnav as XPathNavigator
Try
Xpathdoc = New XPathDocument ("C:\builder.xml")
Xmlnav = Xmldoc.createnavigator ()
Catch ex as XPathException
System.Console.WriteLine ("XmlException:" + ex.) Message)
Catch ex as Exception
System.Console.WriteLine ("Exception:" + ex.) Message)
End Try

Note that the code used to access the XML document is enclosed in a try/catch block to handle any run-time exceptions that may occur. Also note that you need to include the following namespaces to take advantage of XML classes in your own code:

Imports System.Xml.XPath

This namespace allows you to directly use the class you want without having to include the fully qualified name. In addition to the classes used to date, the XPathNodeIterator and XPathExpression classes are used when working with XML data.

XML Data Navigation

The XPathNodeIterator class includes the following methods for traversing an XML document:

    • moveto--go to a specific node within an XML document
    • movetonext--go to the next sibling node, the current node as the base node
    • movetoprevious--go to the previous sibling node, the current node as the base node
    • movetoroot--go to the root node of an XML document
    • movetoparent--go to the parent node of the current node

The following demo code shows how to apply these methods:

Dim Xpathdoc as XPathDocument
Dim Xmlnav as XPathNavigator
Dim Xmlni as XPathNodeIterator
Xpathdoc = New XPathDocument ("C:\builder.xml")
Xmlnav = Xpathdoc.createnavigator ()
Xmlni = Xmlnav.select ("/sites/website")
while (Xmlni.movenext ())
System.Console.WriteLine (XmlNI.Current.Name + ":" + xmlNI.Current.Value)
End While

The preceding code traverses all nodes returned by an XPath expression, such as/sites/website. The expression returns all website nodes and treats the Sites node as the parent node (that is, the root node). The MoveNext method can be used by the XPathNodeIterator base class, but the other methods listed earlier require a node as a starting point. The current node is accessed using the XPathNodeIterator class's present attribute.

The following demo code extends the example above, using the MovePrevious method to reverse traverse the list of nodes after the initial loop:

Dim Xpathdoc as XPathDocument
Dim Xmlnav as XPathNavigator
Dim Xmlni as XPathNodeIterator
Xpathdoc = New XPathDocument ("C:\builder.xml")
Xmlnav = Xpathdoc.createnavigator ()
Xmlni = Xmlnav.select ("/sites/website")
while (Xmlni.movenext ())
System.Console.WriteLine (XmlNI.Current.Name + ":" + xmlNI.Current.Value)
End While
Todo
System.Console.WriteLine (XmlNI.Current.Name + ":" + xmlNI.Current.Value)
Loop while (XmlNI.Current.MoveToPrevious ())

You can iterate through the search results in other ways. In addition, a large number of properties can be used to determine the characteristics of a node. The following is a brief summary:

    • hasattributes--indicates whether a node has a attributes Boolean value
    • haschildren--A Boolean value that indicates whether a node has child nodes
    • isemptyelement--A Boolean value indicating whether the current node is empty
    • name--the name of the current node
    • nodetype--node type
    • value--the text value corresponding to the node

The following code uses the 2 properties above to determine the characteristics of a node:

while (Xmlni.movenext ())
If not (xmlNI.Current.IsEmptyElement) Then
System.Console.WriteLine (XmlNI.Current.Name + ":" + xmlNI.Current.Value)
End If
If not (XmlNI.Current.HasAttribute) Then
Work with attributes
End If
End While

Of course, using XPath makes it easy to manipulate nodes. The following expression returns a node with a specific attribute value:

/sites/website[@Type = "Technology News"]

Locate the data you want

The XPath specification simplifies the navigation of XML documents, and the examples in this article prove that it is very easy to start with.



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.