ASP.Net Repeater Bind Dynamic XML DataSource Using C#

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

In ASP.Net 2.0 you can use XMLDataSource class as a dynamic xml data web control to bind the xml data with Repeater control dynamically using C# code. Create an object of XMLDataSource class to allocate memory for XML data retrieved from the DataSource. Pass the values for DataFile property and XPath property of XMLDataSource and bind the data object to Repeater Control to render the XML data items on ASP.Net web page. While binding the XML data with Repeater or any Data Control such as GridView or DataList you have to use IXPathNavigable databinding syntax inside the ItemTemplate. For SQL Data based DataSet you generally use DataBinder class method Eval but for XML DataSource you have to use XPath, XPathSelect or XPathBinder methods to display the xml data items retrieved using XMLDataSource control dynamically.

 

ASP.Net XML DataSource IXPathNavigable Databinding

Following are the XML data binding syntax that implements IXPathNavigable interface to display the data items:

 

XPath: XPath expression method evaluates the XPath query expression passed to it and returns a single result value of XML element or attribute.

 

XPathBinder: XPathBinder Class provides the Eval function that returns the single string type value evaluated from XPath query expression passed to it.

 

XPathSelect: XPathSelect function evalautes the XPath query expression and returns the IEnumerable type list of xml nodes.

 

C# Code for ASP.Net Repeater Bind Dynamic XML DataSource

 

XmlDataSource xmlSource = new XmlDataSource();

xmlSource.DataFile = "~/App_Data/movies.xml";

xmlSource.XPath =
"movieList/movie";

Repeater1.DataSource = xmlSource;

Repeater1.DataBind();

 

 

XML File for this 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>

Save above xml data file with name movies.xml

 

HTML source for Repeater Control with XPath Expressions

 

<asp:Repeater ID="Repeater1" runat="server">

<ItemTemplate>

<%# XPath("@id") %>

<br />

<%# XPathBinder.Eval(Container.DataItem,"title") %>

<br />

</ItemTemplate>

<SeparatorTemplate><hr /></SeparatorTemplate>

</asp:Repeater>

 

 

Output:

 

Add comment


(Will show your Gravatar icon)  

  Country flag

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



Live preview

8/23/2008 9:05:42 PM

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