Click here to Skip to main content
13,096,858 members (84,891 online)
Click here to Skip to main content


72 bookmarked
Posted 27 Aug 2002

XPath Analyzer

, 27 Aug 2002
Online tool to analyze XPath query
<%@ Import Namespace="System.Data" %>
<%@ Import Namespace="System.Xml" %>
<%@ Import Namespace="System.Xml.XPath" %>

<script language="VB" runat="server">

Dim intIndent as Integer = 0
Dim xDoc as XPathDocument
Dim xNav as XPathNavigator

Sub btnAnalyze_Click(obj as Object, e as EventArgs)
	Dim xNodeIterator as XPathNodeIterator
	Dim objTable as DataTable
	Dim objRow as DataRow
	Dim strAttribute as String
	Dim intTotalNode as Integer = 0
	Dim strNodeType as String

		'Instantiate XPathDocument and load XML document
		xDoc = New XPathDocument(Server.MapPath(txtXML.Text))

		'Instantiate XPathNavigator
		xNav = xDoc.CreateNavigator()

		'Create temporary table to keep query result
		objTable = New DataTable("QueryResult")
		objTable.Columns.Add("node", Type.GetType("System.String"))
		objTable.Columns.Add("value", Type.GetType("System.String"))
		objTable.Columns.Add("attribute", Type.GetType("System.String"))

		'Execute XPath query
		xNodeIterator = xNav.Select(txtXPath.Text)

		'Iterate through the resultant node set
		While xNodeIterator.MoveNext()

			'Count the number of nodes in the node set
			intTotalNode += 1

			'Get the current node as XPathNavigator object
			xNav = xNodeIterator.Current

			'Create a new row in the temporary table
			objRow = objTable.NewRow()

			'Fill the first column of new row
			Select Case xNav.NodeType
				Case XPathNodeType.Element
					strNodeType = "&lt;&gt;"
				Case XPathNodeType.Attribute
					strNodeType = "="
				Case XPathNodeType.Text
					strNodeType = "Abc"
				Case XPathNodeType.Comment
					strNodeType = "&lt;!&gt;"
				Case Else
					strNodeType = ""
			End Select
			objRow("node") = "<font color=""red""><b>" & strNodeType & "</b></font> " & xNav.Name

			'Fill the second column of new row
			If xNav.MoveToFirstChild() Then
				objRow("value") = RenderTree()
				objRow("value") = xNav.Value
			End If

			'Fill the third column of new row
			strAttribute = ""
			If xNav.NodeType = XPathNodeType.Attribute Then
				objRow("attribute") = "n/a"
				'Iterate through attributes if any
				If xNav.MoveToFirstAttribute() Then
						strAttribute += xNav.Name & ": " & xNav.Value & "<br>"
					Loop While xNav.MoveToNextAttribute()
					objRow("attribute") = strAttribute
				End If
			End If

			'Effectively add the new row to the temporary table
		End While

		lblMessage.Text = "Total nodes found: " & intTotalNode.ToString()

		'Bind data grid
		objRepeater.DataSource = objTable.DefaultView

	Catch ex as Exception
		lblMessage.Text = "<font class=""error"">" & ex.Message & "</font>"
	End Try
End Sub

Function RenderTree() As String
	Dim strRenderTree As String

	'Iterate through all siblings
		Select Case xNav.NodeType
			Case xPathNodeType.Text
				strRenderTree += xNav.Value
			Case xPathNodeType.Comment
				strRenderTree += "&lt;!-- " & xNav.Value & "--&gt;<br>"
			Case Else
				'Render indent
				strRenderTree += RenderHTMLSpace(intIndent)

				'Render opening tag
				strRenderTree += "&lt;" & xNav.Name

				'Render attributes if any
				If xNav.MoveToFirstAttribute() Then
						strRenderTree += " " & xNav.Name & "= """ & xNav.Value & """"
					Loop While xNav.MoveToNextAttribute()
				End If
				strRenderTree += "&gt;"

				'Render child nodes if any
				If xNav.MoveToFirstChild() Then
					If xNav.NodeType <> xPathNodeType.Text Then
						strRenderTree += "<br>"
					End If

					intIndent += 2

					'Invoke this function recursively
					strRenderTree += RenderTree()

					intIndent -= 2

					'Move back to the parent node
				End If

				'Render closing tag
				strRenderTree += "&lt;/" & xNav.Name & "&gt;" & "<br>"
		End Select
	Loop While (xNav.MoveToNext())
	RenderTree = strRenderTree
End Function

Function RenderHTMLSpace(byVal intTotalSpace as Integer) as String
	Dim intLoop as Integer
	Dim strHTMLSpace as String
	For intLoop = 1 to intTotalSpace
		strHTMLSpace += "&nbsp;"
	RenderHTMLSpace = strHTMLSpace
End Function


<title>XPath Analyzer</title>
body {
  font-size: 10pt;
  font-family: verdana,helvetica,arial,sans-serif;
  color: #000000;

font.title {
  font-size: 14pt;
  font-weight: bold;
  font-family: verdana,helvetica,arial,sans-serif;
  color: #000000;

td.heading {
  color: #ffffff;
  font-size: 10pt;
  font-family: verdana,helvetica,arial,sans-serif;

td.normal {
  color: #000000;
  font-size: 8pt;
  font-family: verdana,helvetica,arial,sans-serif;

td.alternating {
  color: #000000;
  font-size: 8pt;
  font-family: verdana,helvetica,arial,sans-serif;

.textbox {
  font-size: 8pt;
  font-family: verdana,helvetica,arial,sans-serif;

.button {
	border: 1px solid #000000;
	background-color: #ffffff;

.error {
  font-size: 10pt;
  font-family: verdana,helvetica,arial,sans-serif;
  color: #ff0000;

<form runat="server">
    <td colspan="2"><font class="title">XPath Analyzer</font></td>
    <td class="heading">XML Virtual Path:</td>
	<td><asp:textbox class="textbox" id="txtXML" columns="30" runat="server" /></td>
    <td class="heading">XPath String:</td>
	<td><asp:textbox class="textbox" id="txtXPath" columns="75" runat="server" /></td>
    <td colspan="2"><asp:button class="button" id="btnAnalyze" onClick="btnAnalyze_Click" text="Analyze" runat="server" /></td>
<asp:label id="lblMessage" EnableViewState="false" runat="server" />
<asp:repeater id="objRepeater" EnableViewState="false" runat="server">
    <table border="1" cellpadding="2" cellspacing="0" width="100%" style="border-collapse: collapse" bordercolor="#000000">
	    <td class="heading">Node</td>
		<td class="heading">Value/Descendant</td>
		<td class="heading">Attributes</td>
	    <td class="normal" valign="top"><%# Container.DataItem("node") %></td>
		<td class="normal" valign="top"><%# Container.DataItem("value") %></td>
		<td class="normal" valign="top"><%# Container.DataItem("attribute") %></td>
	    <td class="alternating" valign="top"><%# Container.DataItem("node") %></td>
		<td class="alternating" valign="top"><%# Container.DataItem("value") %></td>
		<td class="alternating" valign="top"><%# Container.DataItem("attribute") %></td>

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.


This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

A list of licenses authors might use can be found here


About the Author

Enrico Elizar Samuel
Web Developer
Singapore Singapore
Currently, he is in Singapore doing a contract work for a multinational company. During weekend, he is busy exploring Singapore with his lovely fiance to find the best food.

You may also be interested in...

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web04 | 2.8.170813.1 | Last Updated 28 Aug 2002
Article Copyright 2002 by Enrico Elizar Samuel
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid