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

x
?
Solved

xmlNodelist with repeater in asp.net

Posted on 2007-07-19
7
Medium Priority
?
2,698 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>
0
Comment
Question by:simshp
6 Comments
 
LVL 11

Expert Comment

by:DotNetThinker
ID: 19526246
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.
0
 
LVL 25

Expert Comment

by:DBAduck - Ben Miller
ID: 19528025
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
0
 

Author Comment

by:simshp
ID: 19529930
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 ??
0
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

by:
DBAduck - Ben Miller earned 1000 total points
ID: 19531877
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.
0
 
LVL 1

Expert Comment

by:Computer101
ID: 20343719
Forced accept.

Computer101
EE Admin
0
 

Expert Comment

by:aheadlabs
ID: 23186759
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
0

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