In ASP.Net 2.0 you can bind the XML data with Repeater control using XmlDataSource control. As we discussed in the previous article about ASP.Net XML DataSource XPath and ASP.Net 2.0 XmlDataSource Control that XmlDataSource control can read the XML document and returns the set of xml nodes according to its XPath property expression value. DataFile property accepts the URL path of the xml file and selects the nodes specified in the XPath query expression passed to the XPath property. When you binding XmlDataSource control with Repeater Control it provides the XPath data binder that implements IXPathNavigable Interface to display the xml data items retrieved from the XML document.
Use XML file "movies.xml":
<movieList>
<title>Bad Boys</title>
</movie>
<movie id="2">
<title>Chronicles of Narnia</title>
<movie id="3">
<title>The Love Guru</title>
</movieList>
<asp:XmlDataSource
ID="XmlDataSource1"
runat="server"
DataFile="~/App_Data/movies.xml"
</asp:XmlDataSource>
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1">
<ItemTemplate>
</ItemTemplate>
<SeparatorTemplate>
<hr />
</asp:Repeater>
In the above sample code you can see that DataFile property of XmlDataSource has value equal to the path of XML file which is stored in the App_Data directory for data files in ASP.Net 2.0. XPath property contains the expression "movieList/movie" that selects all movie elements from the provided xml file. To bind the XmlDataSource control with Repeater Control DataSourceID property has been assigned with a value as ID of the XmlDataSource Control i.e. XmlDataSoure1. <%# XPath("@id") %> and <%# XPath("title") %> is used inside the ItemTemplate of the Repeater control. Here XPath reads the single data item e.g.: child nodes of movie element from the collection of nodes, according to the iterative item index from the list of nodes retrieved from the XmlDataSource. You can see that no C# code is required while using ASP.Net XmlDataSource control that parses the Xml data quickly and can be displayed in any ASP.Net Data Controls like Repeater, GridView and DataList.
Output:
Be the first to rate this post
Tags: asp.net 2.0, xml, asp.net repeater control, asp.net xmldatasource datafile, asp.net xmldatasource xpath, asp.net xmldatasource control, asp.net repeater databinding, asp.net xpath expressions
8/23/2008 9:07:11 PM