Click here to Skip to main content
15,895,667 members
Articles / Web Development / ASP.NET

XPath Analyzer

Rate me:
Please Sign up or sign in to vote.
4.96/5 (15 votes)
27 Aug 20027 min read 146K   2.2K   72  
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

	Try
		'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()
				xNav.MoveToParent()
			Else
				objRow("value") = xNav.Value
			End If

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

			'Effectively add the new row to the temporary table
			objTable.Rows.Add(objRow)
		End While

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

		'Bind data grid
		objRepeater.DataSource = objTable.DefaultView
		objRepeater.DataBind()

	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
	Do
		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
					Do
						strRenderTree += " " & xNav.Name & "= """ & xNav.Value & """"
					Loop While xNav.MoveToNextAttribute()
					xNav.MoveToParent()
				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
					xNav.MoveToParent()
				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;"
	Next
	RenderHTMLSpace = strHTMLSpace
End Function

</script>

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

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;
  background-color:#900B08;
}

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

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

.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;
}
</style>

<body>
<form runat="server">
<table>
  <tr>
    <td colspan="2"><font class="title">XPath Analyzer</font></td>
  </tr>
  <tr>
    <td class="heading">XML Virtual Path:</td>
	<td><asp:textbox class="textbox" id="txtXML" columns="30" runat="server" /></td>
  </tr>
  <tr>
    <td class="heading">XPath String:</td>
	<td><asp:textbox class="textbox" id="txtXPath" columns="75" runat="server" /></td>
  </tr>
  <tr>
    <td colspan="2"><asp:button class="button" id="btnAnalyze" onClick="btnAnalyze_Click" text="Analyze" runat="server" /></td>
  </tr>
</table>
<br>
<asp:label id="lblMessage" EnableViewState="false" runat="server" />
<asp:repeater id="objRepeater" EnableViewState="false" runat="server">
  <HeaderTemplate>
    <table border="1" cellpadding="2" cellspacing="0" width="100%" style="border-collapse: collapse" bordercolor="#000000">
	  <tr>
	    <td class="heading">Node</td>
		<td class="heading">Value/Descendant</td>
		<td class="heading">Attributes</td>
	  </tr>
  </HeaderTemplate>
  <ItemTemplate>
      <tr>
	    <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>
	  </tr>
  </ItemTemplate>
  <AlternatingItemTemplate>
      <tr>
	    <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>
	  </tr>
  </AlternatingItemTemplate>
  <FooterTemplate>
    </table>
  </FooterTemplate>
</asp:repeater>
</form>
</body>
</html>

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.

License

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


Written By
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.

Comments and Discussions