Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium


xmlNodelist with repeater in asp.net

Posted on 2007-07-19
Medium Priority
Last Modified: 2013-11-18
I have a an XML string called xmlList =

I would like to bind it to a asp:repeater control and access these elements.

I use this psudo code:

xmlDoc xmlDocument()

XmlNodeList dataSourceNodeList = xmlDoc.SelectNodes("DataList/DataItem");

rpListItems.DataSource = dataSourceNodeList

What syntax do I use to display particular items in each node - like so?:

<asp:Repeater id="rpListItems" runat="server">
<td><%# ((XmlNode)Container.DataItem).(DataItem/name) %></td>
<td><%# ((XmlNode)Container.DataItem).(DataItem/email) %></td>
Question by:simshp
LVL 11

Expert Comment

ID: 19526246
try this...

<asp:Label Id="lblName" runat="server" Text='<%# Bind("name") %>'></asp:Label>
<asp:Label Id="lblEmail" runat="server" Text='<%# Bind("email") %>'></asp:Label>

If you want more of a table look you should think about using a gridview instead.
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 19528025
You should be able to do something like this:

A little variation of above.

<asp:Label Id="lblName" runat="server" Text='<%# Eval("name") %>'></asp:Label>
<asp:Label Id="lblEmail" runat="server" Text='<%# Eval("email") %>'></asp:Label>


Author Comment

ID: 19529930
Tried both methods to no avail. Solved the problem using a dataset:

            Dim ds As DataSet = New DataSet
            ds.ReadXml(New StringReader(xmlList))

            rpDirectoryItems.DataSource = ds

and the following to bind in the repeater :

'<%# DataBinder.Eval(Container, "DataItem.Name") %>'
'<%# DataBinder.Eval(Container, "DataItem.Email") %>'

Would be nice to know how to bind using only xml and xpaths :

            'xmlDoc = New XmlDocument

Dim xmlDirSummaryList As XmlNodeList = xmlDoc.SelectNodes("/DirSummaryList/DirSummaryItem")
          rpDirectoryItems.DataSource = xmlDirSummaryList

How to bind in the aspx file ??
Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

LVL 25

Accepted Solution

DBAduck - Ben Miller earned 1000 total points
ID: 19531877
That would work, but you could also look at the XmlDataSource as well.


That is if the data is in a file, which it apparently is not according to your code above.  So if you saved it in a file, then you could, but otherwise, I do not believe that you can DataBind XML directly according to the documentation.  Next time I will test the code before suggesting :-).

The DataSet way is the most common way to get this done.


Expert Comment

ID: 20343719
Forced accept.

EE Admin

Expert Comment

ID: 23186759
Having this XML:

<book id="1" name="Ivan"/>
<book id="1" name="Pedro"/>

You can bind an XmlNodeList (the name attribute in this example) in an asp:Repeater element using the following expression:
<%# DataBinder.Eval(((System.Xml.XmlNode)(Container.DataItem)).Attributes["name"], "Value") %>

Regards ;),
Iván Sainz

Featured Post

Concerto Cloud for Software Providers & ISVs

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!

Learn how Concerto can help you.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

JavaScript has plenty of pieces of code people often just copy/paste from somewhere but never quite fully understand. Self-Executing functions are just one good example that I'll try to demystify here.
International Data Corporation (IDC) prognosticates that before the current the year gets over disbursing on IT framework products to be sent in cloud environs will be $37.1B.
Viewers will learn about the regular for loop in Java and how to use it. Definition: Break the for loop down into 3 parts: Syntax when using for loops: Example using a for loop:
Learn how to create flexible layouts using relative units in CSS.  New relative units added in CSS3 include vw(viewports width), vh(viewports height), vmin(minimum of viewports height and width), and vmax (maximum of viewports height and width).
Suggested Courses

572 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question