Signup · Login
Stardeveloper.com  
Home · Tutorials · Forums · ASP.NET Newsletter Application · Web Hosting Plans · Faisal Khan's Blog · Contact
Search Stardeveloper.com
Newsletter
Enter your email address to receive full length articles at Stardeveloper:


Article Categories
.NET  .NET
  ASP (16)
  ASP.NET (43)
  ADO (16)
  ADO.NET (11)
  COM (6)
  Web Services (4)
  C# (1)
  VB.NET (3)
  IIS (2)

J2EE  J2EE
  JSP (15)
  Servlets (9)
  Web Services (1)
  EJB (4)
  JDBC (4)
  E-Commerce (1)
  J2ME (1)
  Products (1)
  Applets (1)
  Patterns (1)

Main Category  Other
  Website Maintenance (3)
Log In
UserName Or Email:

Password:

Auto-Login:

Hosted by Securewebs.com
 
Home : .NET : ASP.NET : Introduction to XPath for ASP.NET Developers
 
Read full length articles at Stardeveloper using Twitter Follow on Twitter Facebook Facebook fan page Email Get Articles via Email RSS Get Articles via RSS Feed

Introduction to XPath for ASP.NET Developers

by Faisal Khan.

Introduction
XPath is a language for selecting nodes (parts or segments) within an XML document. As you are already familiar with XML, XML is a markup language which uses elements and attributes to encapsulate data in a logical manner. XPath furthers builds on it and provides navigational abilities in an XML document. For example, you can use an XPath query to select one or more nodes within an XML document that match a certain criteria. That criteria can by anything from an element matching a given name to an element whose attribute matches a specific value.

This tutorial is 3rd one in the series of tutorials about XML and ASP.NET:

Tree like representation of an XML Document
In XPath, an XML document is represented as a tree of nodes. There is a parent node with one or more child nodes. While a node in XPath can be of 7 types; for practical purposes, a node in XPath corresponds to an element or attribute within an XML document. For example, have a look at following XML document:

<?xml version="1.0" encoding="utf-8" ?>
<userinfo>
	<username admin="true">someUserName1</username>
	<email>xyz@whatever.com</email>
</userinfo>

Following is XPath's tree representation of the above document:

  • userinfo
    • username
      • admin
    • email

Each node is related to the nodes above and below it. In our XML document above, userinfo is root node. userinfo is the parent of username and email nodes. username and email nodes are siblings. username and email are children of userinfo node. Similarly, admin is the child of username node.

Selecting Nodes within an XML Document
Now that we understand how elements and attributes are represented as nodes in XPath, we will focus on how to use XPath expressions to select one or more nodes within an XML document.

XPath Expressions
Following are some of the expressions that you can use to select one or more nodes from the XML document above:

  • /userinfo - Selects the root element.
  • /userinfo/username - Selects the username node which is the child of userinfo root node.
  • //email - Selects all the nodes in the document which match the name (email) irrespective of where they lie in the document.
  • //username[@admin] - Selects all nodes with the name of "username" which have an attribute; "admin".
  • /userinfo/username[1] - Selects the first username node that is the child of userinfo node.
  • /userinfo/username[last()] - Assuming userinfo had more than one username child nodes, it will return the last username node that is the child of userinfo node.

Practical Demonstration of XPath Expressions
We will now create a sample XML document and then use XPath expressions to select and display only few nodes from it.

Sample XML Document
Copy and paste following text in a new text file and save it as "sample.xml" in the /App_Data folder of your ASP.NET web application:

<?xml version="1.0" encoding="utf-8" ?>
<article>
	<author isadmin="true">Faisal Khan</author>
	<title>Sample XML Document</title>
	<body>
		<page>This is page #1.</page>
		<page>This is page #2.</page>
		<page>This is page #3.</page>
	</body>
</article>

XPath.aspx ASP.NET Page
Now, we will create the ASP.NET page which will read the above sample.xml file and selectively display its contents using XPath. Copy and paste the following text into a new text file and save it as "XPath.aspx" in your ASP.NET web application:

<%@ Page Language="C#" AutoEventWireup="true" %>

<%@ Import Namespace="System.Xml" %>

<script runat="server">
	protected void Page_Load(object source, EventArgs e)
	{
		XmlDocument doc = new XmlDocument();
		doc.Load(Server.MapPath("~/App_Data/sample.xml"));

		XmlNodeList nodes = doc.SelectNodes("/article/body/page");

		foreach (XmlNode node in nodes)
		{
			TableRow row = new TableRow();
			TableCell cell = new TableCell();
			cell.Text = node.FirstChild.InnerText;

			row.Cells.Add(cell);
			PagesTable.Rows.Add(row);
		}
	}
</script>

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
	"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">

<html xmlns="http://www.w3.org/1999/xhtml">
<head runat="server">
    <title>Using XPath Expressions</title>
    <style type="text/css">
		body { font-family: Verdana; font-size: 9pt; }
		.name { background-color: #F7F7F7; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
		<asp:Table id="PagesTable" runat="server" />
    </div>
    </form>
</body>
</html>

We will look into its code a little later, for now when this ASP.NET page was run on my computer, it produced following result (displaying only page elements from the XML file):

Using XPath Expressions
Using XPath Expressions

 ( 1 Remaining ) Next

Comments/Questions

No Comments Found.


Post Comments/Questions

In order to post questions/comments, you must be logged-in. If you are not a member yet, then signup, otherwise login. Once you login then come back to this page and you'll see a form right here which will allow you to post comments/questions.

Please note, one of the benefits of signing up is to be notified immediately by email everytime you receive a reply to the thread you have subscribed.

 
© 1999 - 2010 Stardeveloper.com, All Rights Reserved.