ASP.Net XML DataSource XPath

by top54u.com 11 Jul, 2008
Spotlight.....

XMLDataSource control of ASP.Net has a property XPath that takes the value as XPath query to read the XML Nodes list under that specified XML path. XPath query works like SQL Select query that can select the XML nodes or a single XML node based on the syntax of XPath expression passed. For XMLDatasource control you can set the path of XML data file into its DataFile property and XPath value can be set according to the elements of the specified XML file.

 

XML File Syntax Example

 

<?xml version="1.0" encoding="utf-8" ?>

<movieList>

<movie id="1">

<title>Bad Boys</title>

</movie>

<movie id="2">

<title>Chronicles of Narnia</title>

</movie> <movie id="3">

<title>The Love Guru</title>

</movie>

</movieList>

 

In the above XML file code movieList is a root node and movie is child node element. movie element contains id attribute and title child node. movie node is repeated again and again to store the hierarchical data in XML file with movie topics. You can add more item nodes inside the movie element to store more information about movie like we have stored its title.

 

Syntax for ASP.Net XmlDataSource XPath

XPath query expression accepts the following path expressions to select the nodes from the XML document:

 

Expression Description
RootNodeName Selects all child nodes of the root node e.g.: movieList in above XML file is a root node.
/ Single “/” selects the child nodes of the specified node. Single / expression syntax without node name returns all the child nodes of root node.


If expression starts with / then you must specify the root node name at its right side. E.g.: / movieList.


To select child nodes of movie node, you can use: movies/movie
// Double “//” selects all occurrences of specified node in the XML document.
. It returns the current node.
.. It returns the parent node of the current node.
@ It returns the attribute.

Add comment


(Will show your Gravatar icon)  

  Country flag

[b][/b] - [i][/i] - [u][/u]- [quote][/quote]



Live preview

8/23/2008 8:40:50 PM

OUR SPONSORS[+ advertise here]
related videos.....
recent posts.....
top54u ezines.....