How to Bind Nested XML to a Repeater Control with Container.DataItem | CodeGuru

How to Bind Nested XML to a Repeater Control with Container.DataItem

Introduction The container.DataItem is an easy-to-use method to bind XML to a control. You can use a repeater, list control, or any other control to bind the XML. If you have have worked with XML and a repeater control, you know what I am talking about. In this short “how to,” I will show you […]

Written By
CodeGuru Staff
CodeGuru Staff
May 17, 2004
2 minute read
CodeGuru content and product recommendations are editorially independent. We may make money when you click on links to our partners. Learn More

Introduction

The container.DataItem is an easy-to-use method to bind XML to a control. You can use a repeater, list control, or any other control to bind the XML. If you have have worked with XML and a repeater control, you know what I am talking about. In this short “how to,” I will show you how you can bind a nested XML element to a repeater control. But, first of all, let’s see an example where we bind a simple XML to a repeater control.

Simple Data Binding

This is a very famous and simple method to retrive data with container.DataItem.

XML file

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
   <Category>
      <MainCategory>XML</MainCategory>
      <SubCategory>Basic</SubCategory>
      <Description>List of XML articles.</Description>
      <Active>Yes</Active>
   </Category>
</CategoryList>

Code

DataSet ds = new DataSet();
ds.ReadXml(MapPath("myfile.xml"));
rpMyRepeater.DataSource=ds;
rpMyRepeater.DataBind();

Now, let me show you how to use ASP.NET to retrieve the data from XML.

<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>
   <Table border="0">
</HeaderTemplate>
   <ItemTemplate>
   <tr style="background-color:FFECD8">
      <td>
         <%# DataBinder.Eval(Container.DataItem, "MainCategory") %>
         <%# DataBinder.Eval(Container.DataItem, "Description") %>
      </td>
   </tr>
   </ItemTemplate>
   <FooterTemplate>
      </Table>
   </FooterTemplate>
</asp:repeater>

As you can see, you don’t need much code to bind the XML. Unfortunately, this method works only if you have am XML structure such as the one shown above. So, how do you use the Container.DataItem to retrieve attributes? Let us change our XML file a little bit.

Advertisement

New XML file

<?xml version="1.0" encoding="utf-8"?>
<CategoryList>
   <Category>
      <MainCategory ID="1">XML</MainCategory>
      <SubCategory>Basic</SubCategory>
      <Description>List of XML articles.</Description>
      <Active>Yes</Active>
   </Category>
</CategoryList>

When you run the above code with the new XML file, you will see that this time it throws the following exception:

System.Web.HttpException: DataBinding: 'System.Data.DataRowView'
does not contain a property with the name 'MainCategory'.

The problem is that you now are using an attribute in the XML file; therefore, you can not use the above code. Now, let us try to retrieve the ID and the MainCategory element’s value. For that purpose, you will need to change a little bit of the code. Your new code should look like this:

XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("dbase/categories.xml"));
XmlNodeList nodes =
   doc.SelectNodes("CategoryList/Category/MainCategory");
rpMyRepeater.DataSource = nodes;
rpMyRepeater.DataBind();

This time, we are not using a dataset to load the XML file. Instead, we will have to load the XML file into a XmlDocument and use the SelectNodes method to retrieve the nodes. The nodes then are passed to the repeater control. And, here is the trick to get the XML data with Container.DataItem.

<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>
   <Table border="0">
</HeaderTemplate>
   <ItemTemplate>
   <tr style="background-color:FFECD8">
      <td>
         <%#((System.Xml.XmlNode)Container.DataItem).
             Attributes["ID"].Value %>
         <%#((System.Xml.XmlNode)Container.DataItem).InnerText%>
      </td>
   </tr>
   </ItemTemplate>
   <FooterTemplate>
      </Table>
   </FooterTemplate>
</asp:repeater>

I would like to thank Kirk Allen Evan for this great tip. See his blog entry about that: http://blogs.msdn.com/kaevans/archive/2003/07/04/9713.aspx.

CodeGuru Logo

CodeGuru covers topics related to Microsoft-related software development, mobile development, database management, and web application programming. In addition to tutorials and how-tos that teach programmers how to code in Microsoft-related languages and frameworks like C# and .Net, we also publish articles on software development tools, the latest in developer news, and advice for project managers. Cloud services such as Microsoft Azure and database options including SQL Server and MSSQL are also frequently covered.

Property of TechnologyAdvice. © 2026 TechnologyAdvice. All Rights Reserved

Advertiser Disclosure: Some of the products that appear on this site are from companies from which TechnologyAdvice receives compensation. This compensation may impact how and where products appear on this site including, for example, the order in which they appear. TechnologyAdvice does not include all companies or all types of products available in the marketplace.