Use XPath to navigate XML document (VB.net) : XPath « XML « ASP.NET Tutorial






<%@ Page Language="VB" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>

<script runat="server">
   sub Page_Load(Sender as object, e as EventArgs)
      Dim objReader as New XmlTextReader(Server.MapPath("Data.xml"))

      Dim objDoc as XPathDocument = new XPathDocument(objReader)
      Dim objNav as XPathNavigator = objDoc.CreateNavigator()

      DisplayNode(objNav)
   end sub

   sub DisplayNode(objNav as XPathNavigator)
      If objNav.HasChildren then
         objNav.MoveToFirstChild()
         Format(objNav)
         DisplayNode(objNav)
         objNav.MoveToParent()
      End If

      While objNav.MoveToNext()
         Format(objNav)
         DisplayNode(objNav)
      end While
   end sub

   Private Sub Format(objNav As XPathNavigator)
      If Not objNav.HasChildren then
         if objNav.NodeType <> XmlNodeType.Text then
            Response.Write("&lt;<b>" & _
               objNav.Name & "</b>&gt;")
         end if
         Response.Write("&nbsp;- " & objNav.Value & _
               "<br>" & vbCrLf)
      Else
         Dim i As Integer
         Response.Write("&lt;<b>" & objNav.Name & _
            "</b>&gt;" & vbCrLf)

         If objNav.HasAttributes then
            Response.Write("<br>Attributes of &lt;" & _
               objNav.Name & "&gt;<br>" & vbCrLf)
         End If

         while objNav.MoveToNextAttribute()
            Response.Write("&lt;<b>" & objNav.Name & "</b>&gt; " & objNav.Value & " ")
         end while
         Response.Write("<br>" & vbCrLf)
      End If
   end sub
</script>

<html><body>
</body></html>








25.17.XPath
25.17.1.Use XPath to navigate XML document (VB.net)
25.17.2.Use XPath to query XML data (VB.net)
25.17.3.XPath Navigator Read
25.17.4.XPath Search
25.17.5.Querying XML with XPathDocument and XPathNodeIterator (C#)
25.17.6.Querying XML with XPathDocument and XPathNodeIterator (VB)