Mega Code Archive

 
Categories / ASP.Net Tutorial / XML
 

Use XPath to navigate XML document (VB net)

<%@ 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>