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.
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.
xmlSource.DataFile = "~/App_Data/movies.xml";
Repeater1.DataSource = xmlSource;
Repeater1.DataBind();
XML File for this example:
<movieList>
<title>Bad Boys</title>
<movie id="2">
</movie>
<title>The Love Guru</title>
</movieList>
Save above xml data file with name movies.xml
<asp:Repeater ID="Repeater1" runat="server">
<ItemTemplate>
<br />
</ItemTemplate>
</asp:Repeater>
Output:
Be the first to rate this post
Tags: asp.net 2.0, xml, c#, asp.net repeater control, asp.net xmldatasource datafile, asp.net xmldatasource xpath, asp.net xmldatasource control, c# code, asp.net repeater databinding, asp.net xpath expressions, asp.net ixpathnavigable interface, asp.net xpath, asp.net xpathselect, asp.net xpathbinder, asp.net dynamic xmldatasource
8/23/2008 9:05:42 PM