Server Intellect
 
Home   Asp.Net Tutorials   What's New   Newsletter   More Resources
 
 
  Categories
Advanced Technologies
AJAX
Internet Browsers
Controls
Database
Email
Error Handling
File
Graphics
Website Navigation
Network
Performance
User Interface and Themes
Validation
Visual Web Developer
Web Services
XML
Suggest Tutorial


Navigator: Home - Tutorials - XML - Navigating XML information items using ASP.NET and C#
Navigating XML information items using ASP.NET and C#


ASP.NET XML Tutorial

This tutorial will show you how to navigate XML information items in ASP.NET 2.0 using C#.

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!

First, import the namespace of System.XML and System.XML.XPath

The System.Xml.XPath namespace contains the classes that define a cursor model for navigating and editing XML information items as instances of the XPath 2.0 Data Model. In this sample, we will use the classes of XPathDocument, XPathNavigator, XPathExpression and XPathNodeIterator under this namespace .

using System.Xml;
using System.Xml.XPath

The Button1_Click event is to perform the navgating XML infomration items fuction. The results will be displayed in the textbox. There are 4 classes we applied.

XPathDocument: Provides a fast, read-only, in-memory representation of an XML document using the XPath data model.

XPathNavigator: Provides a cursor model for navigating XML data.

XPathExpression: Provides a typed class that represents a compiled XPath expression.

XPathNodeIterator: Provides an iterator over a selected set of nodes.

XPathDocument doc = new XPathDocument(Server.MapPath("Demo.xml"));
XPathNavigator nav = doc.CreateNavigator();

XPathExpression xpathExpress = nav.Compile("//bk:Book[position()"
+this.ddownlist.SelectedValue.Trim()+this.txtNum.Text.Trim()+"]");
//use AddNamespace
XmlNamespaceManager xmlManager = new XmlNamespaceManager(nav.NameTable);
xmlManager.AddNamespace("bk", "http://myserver/myschemas/Books");
xpathExpress.SetContext(xmlManager);

XPathNodeIterator xIterator = nav.Select(xpathExpress);
this.TextBox1.Text = "";
this.TextBox1.ForeColor = System.Drawing.Color.Empty;
while (xIterator.MoveNext())
this.TextBox1.Text = this.TextBox1.Text + "\r\n" + xIterator.Current.Value;

if (xIterator.Count.Equals(0))
{
this.TextBox1.Text = "There is no xml data in the condition.";
this.TextBox1.ForeColor = System.Drawing.Color.Red;
}
xmlManager = null;
nav = null;
doc = null;

The contents of Demo.xml:

<?xml version='1.0' encoding='utf-8'?>
<bk:Books xmlns:bk='http://myserver/myschemas/Books'>
<bk:Book>
<bk:Title>Just XML</bk:Title>
</bk:Book> <bk:Book>
<bk:Title>Professional XML</bk:Title>
</bk:Book> <bk:Book>
<bk:Title>XML Step by Step</bk:Title>
</bk:Book> <bk:Book>
<bk:Title>XML By Example</bk:Title>
</bk:Book>
</bk:Books>

The front end Default.aspx page looks something like this:

<body>
<form id="form1" runat="server">
<div>
<fieldset><legend>Select xml data with XPath</legend>
<asp:GridView ID="GridView1" runat="server" AutoGenerateColumns="False" CellPadding="4"
Font-Size="Smaller" ForeColor="#333333" GridLines="None" RowHeaderColumn="Title"
Width="205px">
<FooterStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<Columns>
<asp:BoundField DataField="Title" HeaderText="Title" />
</Columns>
<RowStyle BackColor="#EFF3FB" />
<EditRowStyle BackColor="#2461BF" />
<SelectedRowStyle BackColor="#D1DDF1" Font-Bold="True" ForeColor="#333333" />
<PagerStyle BackColor="#2461BF" ForeColor="White" HorizontalAlign="Center" />
<HeaderStyle BackColor="#507CD1" Font-Bold="True" ForeColor="White" />
<AlternatingRowStyle BackColor="White" />
</asp:GridView> <br />
<asp:Label ID="Label1" runat="server" Text="Select Item Position:"></asp:Label>&nbsp;<asp:DropDownList
ID="ddownlist" runat="server">
<asp:ListItem>&lt;</asp:ListItem>
<asp:ListItem>&lt;=</asp:ListItem>
<asp:ListItem>&gt;</asp:ListItem>
<asp:ListItem>&gt;=</asp:ListItem>
</asp:DropDownList>
<asp:TextBox ID="txtNum" runat="server" Width="57px">2</asp:TextBox>
<asp:RequiredFieldValidator ID="RequiredFieldValidator1" runat="server" ControlToValidate="txtNum"
ErrorMessage="Please input integer number."></asp:RequiredFieldValidator><br />
<asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Search" /><br />
<asp:TextBox ID="TextBox1" runat="server" Height="88px" TextMode="MultiLine" Width="237px"></asp:TextBox>
</fieldset>
</div>
</form>
</body>

The flow for the code behind page as follows.

using System;
using System.Data;
using System.Configuration;
using System.Web;
using System.Web.Security;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.WebControls.WebParts;
using System.Web.UI.HtmlControls;

using System.Xml;
using System.Xml.XPath;
public partial class _Default : System.Web.UI.Page
{
protected void Page_Load(object sender, EventArgs e)
{
DataSet myDs = new DataSet();
myDs.ReadXml(Server.MapPath("Demo.xml"));
this.GridView1.DataSource = myDs;
this.GridView1.DataBind();
}
protected void Button1_Click(object sender, EventArgs e)
{
XPathDocument doc = new XPathDocument(Server.MapPath("Demo.xml"));
XPathNavigator nav = doc.CreateNavigator();

XPathExpression xpathExpress = nav.Compile("//bk:Book[position()"
+this.ddownlist.SelectedValue.Trim()+this.txtNum.Text.Trim()+"]");
//use AddNamespace
XmlNamespaceManager xmlManager = new XmlNamespaceManager(nav.NameTable);
xmlManager.AddNamespace("bk", "http://myserver/myschemas/Books");
xpathExpress.SetContext(xmlManager);

XPathNodeIterator xIterator = nav.Select(xpathExpress);
this.TextBox1.Text = "";
this.TextBox1.ForeColor = System.Drawing.Color.Empty;
while (xIterator.MoveNext())
this.TextBox1.Text = this.TextBox1.Text + "\r\n" + xIterator.Current.Value;

if (xIterator.Count.Equals(0))
{
this.TextBox1.Text = "There is no xml data in the condition.";
this.TextBox1.ForeColor = System.Drawing.Color.Red;
}
xmlManager = null;
nav = null;
doc = null;
}
}

Download the Full Working Version of this Project written with Visual Studio.NET C# 2005 Here!

Looking for the VB.NET 2005 Version? Click Here!

Looking for more ASP.NET Tutorials? Click Here!







 
  Developer Resources







Server Intellect Rocks