ASP.Net XmlDocument XPath C# examples

Updated on 10 Aug 2012,
Published on 16 Jul 2008

ASP.Net XmlDocument class provides the methods to select nodes or a single node by passing XPath expression. Here we will learn the C# code for applying XmlDocument class functions, methods or properties to read the Value from XML file through XmlDataSource. ASP.Net XmlDataSource control reads the xml file and returns the XMLDocument through GetXmlDocument function. Further you can use SelectSingleNode and SelectNodes functions to select the child nodes or their attributes. Now we will use the XPath syntax that we discussed in the previous tutorial about ASP.Net Xml DataSource XPath.

Consider the same XML here:

<?xml version="1.0" encoding="utf-8" ?>
  <movie id="1">
    <title>Bad Boys</title>
  <movie id="2">
    <title>Chronicles of Narnia</title>
  <movie id="3">
    <title>The Love Guru</title>

You can drag and drop the ASP.Net XmlDataSource control on the web page and set its DataFile property as following:

<asp:XmlDataSource ID="XmlDataSource1" 

C# Examples for XmlDocument XPath Query Expressions:

First of all create an XMLDocument class object and assign its reference from the XmlDataSource1 GetXmlDocument function:

XmlDocument xmlDoc = XmlDataSource1.GetXmlDocument();

Use the following C# code to read the XML nodes using different types of XPath expressions:

// Select all child nodes of root node of XML document
Response.Write(xmlDoc.SelectSingleNode("movieList").InnerText + "<br />");
// Select all child nodes of root node of XML document
// using single "/" expression
Response.Write(xmlDoc.SelectSingleNode("/movieList").InnerText + "<br />");

// Select child nodes of movie node under root node movieList
Response.Write(xmlDoc.SelectSingleNode("movieList/movie").InnerText + "<br />");

// Select node value of title node under movie
Response.Write(xmlDoc.SelectSingleNode("movieList/movie/title").InnerText + "<br />");

// Select title child node where ever it exits under movieList root node
Response.Write(xmlDoc.SelectSingleNode("movieList//title").InnerText + "<br />");

// Select title child node where ever it exits in the XML document
Response.Write(xmlDoc.SelectSingleNode("//title").InnerText + "<br />");

// Select id attribute where ever it exits under movieList root node
Response.Write(xmlDoc.SelectSingleNode("//@id").InnerText + "<br />");

// Select root node of XML Document
Response.Write(xmlDoc.SelectSingleNode(".").InnerText + "<br />");

// Select the parent node of title child node [movie]
// Here it returns the count for title child nodes under movie node
Response.Write(xmlDoc.SelectNodes("movieList/movie/title/..").Count + "<br />");

// Returns the Count for title child nodes in the XML Document
Response.Write(xmlDoc.SelectNodes("//title").Count + "<br />");

Output for above C# code examples for XPath expressions passed to the SelectSingleNode and SelectNodes function of XmlDocument class.

Bad BoysChronicles of NarniaThe Love Guru
Bad BoysChronicles of NarniaThe Love Guru
Bad Boys
Bad Boys
Bad Boys
Bad Boys
Bad BoysChronicles of NarniaThe Love Guru

Continue to next tutorial: ASP.Net Repeater Bind Xml DataSource Control to learn how to bind the XML data to Repeater control.

0 Responses to "ASP.Net XmlDocument XPath C# examples"
Leave a Comment
* required
* required
* will not be published
* optional
* hint:
  • Subscribe via Email