We help IT Professionals succeed at work.

We've partnered with Certified Experts, Carl Webster and Richard Faulkner, to bring you a podcast all about Citrix Workspace, moving to the cloud, and analytics & intelligence. Episode 2 coming soon!Listen Now

x

xmlNodelist with repeater in asp.net

Medium Priority
2,815 Views
Last Modified: 2013-11-18
I have a an XML string called xmlList =
"<DataList>
      <DataItem>
            <name>Dave</name>
            <address>London</address>
            <email>s@ss.com</email>
      </DateItem>
      <DateItem>
            <name>Dave</name>
            <address>London</address>
            <email>s@ss.com</email>
      </DateItem>
</DataList>"

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

I use this psudo code:

xmlDoc xmlDocument()
xmlDoc.Loadxml(xmllist)


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


rpListItems.DataSource = dataSourceNodeList
rpListItems.DataBind()

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

<asp:Repeater id="rpListItems" runat="server">
<table>
<ItemTemplate>
<tr>
<td><%# ((XmlNode)Container.DataItem).(DataItem/name) %></td>
<td><%# ((XmlNode)Container.DataItem).(DataItem/email) %></td>
</tr>
</ItemTemplate>
</table>
</asp:Repeater>
Comment
Watch Question

try this...

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

If you want more of a table look you should think about using a gridview instead.
DBAduck - Ben MillerSQL Server Architect
CERTIFIED EXPERT

Commented:
You should be able to do something like this:

A little variation of above.

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

Ben

Author

Commented:
Hi
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
            rpDirectoryItems.DataBind()

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
            'xmlDoc.LoadXml(XmlList)

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

How to bind in the aspx file ??
SQL Server Architect
CERTIFIED EXPERT
Commented:
That would work, but you could also look at the XmlDataSource as well.

http://www.developer.com/db/article.php/3553881

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.

Ben.

Not the solution you were looking for? Getting a personalized solution is easy.

Ask the Experts
Forced accept.

Computer101
EE Admin
Having this XML:

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

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
Access more of Experts Exchange with a free account
Thanks for using Experts Exchange.

Create a free account to continue.

Limited access with a free account allows you to:

  • View three pieces of content (articles, solutions, posts, and videos)
  • Ask the experts questions (counted toward content limit)
  • Customize your dashboard and profile

*This site is protected by reCAPTCHA and the Google Privacy Policy and Terms of Service apply.

OR

Please enter a first name

Please enter a last name

8+ characters (letters, numbers, and a symbol)

By clicking, you agree to the Terms of Use and Privacy Policy.