?
Solved

How can I bind XML file to repeater control in ASP.Net?

Posted on 2011-03-18
10
Medium Priority
?
461 Views
Last Modified: 2012-05-11
Hello experts,

I haven't really worked with XML before.  I need to bind an XML file to a Repeater Control.  I have pasted the XML file below.  In the repeater, I need to display:

Team Name:
TeamIdent:
Team Home:
Member Name 1:
Member Name 2:

Thank you for any help.

Vince

<?xml version='1.0' encoding='utf-8'?>
<round name="main" parent="main" knockout="no">
  <team name="QU" ident="3" pullups="0">
    <home>QATAR</home>
    <positions></positions>
    <member name="Queenie">
      <points>0</points>
      <rounds>0</rounds>
    </member>
    <member name="Queller">
      <points>0</points>
      <rounds>0</rounds>
    </member>
  </team>
  <team name="VCU" ident="1" pullups="0">
    <home>Vancouver</home>
    <positions></positions>
    <member name="Victor">
      <points>0</points>
      <rounds>0</rounds>
    </member>
    <member name="Victoria">
      <points>0</points>
      <rounds>0</rounds>
    </member>
  </team>
  <team name="MUN" ident="2" pullups="0">
    <home>Memorial</home>
    <positions></positions>
    <member name="Mandy">
      <points>0</points>
      <rounds>0</rounds>
    </member>
    <member name="Mary">
      <points>0</points>
      <rounds>0</rounds>
    </member>
  </team>
  <team name="CNAQ" ident="0" pullups="0">
    <home>Newfoundland</home>
    <positions></positions>
    <member name="Feras">
      <points>0</points>
      <rounds>0</rounds>
    </member>
    <member name="Mohd">
      <points>0</points>
      <rounds>0</rounds>
    </member>
  </team>
</round>
0
Comment
Question by:vstack
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 5
  • 4
10 Comments
 
LVL 3

Expert Comment

by:AbdulQuddos
ID: 35164251
.aspx File

<asp:Repeater ID="Repeater1" runat="server">
   <HeaderTemplate >Team Name</HeaderTemplate>
   <ItemTemplate ><%# Eval("team name") %>ItemTemplate>      
   <SeparatorTemplate ></SeparatorTemplate>
asp:Repeater>

.cs File
DataSet ds = New DataSet();
ds.ReadXml(Server.MapPath("YourFileName.xml"))
Repeater1.DataSource = ds
Repeater1.DataBind()

NOTE:
For the .aspx you can add more Item template as per you need for the fields.

Hope this will helps

Thanks
0
 
LVL 3

Expert Comment

by:AbdulQuddos
ID: 35164283
Here is the second possible solution:
http://www.ezzylearning.com/tutorial.aspx?tid=8462553
0
 

Author Comment

by:vstack
ID: 35164761
Thank you.  It looks like the solution in the tutorial might work.  I can display some of the information from the XML file but not all.  Here is what I get:

ID Team      Institution             Debater 1       Debater 2
3   QU           QATAR               00                 00
 
1  VCU         Vancouver             00       00
 
2 MUN          Memorial             00       00
 
0 CNAQ       Newfoundland                    00       00


I want the names of the debaters (member in xml file) to display.  Right now, I just get 00?  I attached XML in original question.

Thank you
<asp:Repeater ID="Repeater1" runat="server" DataSourceID="XmlDataSource1"> 
       <HeaderTemplate>
       <table>
       <tr>
       <td><b>ID</b></td><td><b>Team</b></td><td><b>Institution</b></td>
       <td><b>Debater 1</b></td><td><b>Debater 2</b></td>
       </tr>
       </HeaderTemplate>
   <ItemTemplate>
   
      
     
       <tr><td><%#Eval("ident")%></td><td><%#Eval("name")%></td><td><%#XPath("home")%></td><td><%#XPath("member")%></td><td><%#XPath("member")%></td></tr>
       <tr><td colspan="4"><strong> 
         <%#XPath("@round")%>
               </strong> </td></tr>
       
       
   </ItemTemplate> 
   
   <FooterTemplate>
          </table>
   </FooterTemplate>
</asp:Repeater> 
    </div>
    <asp:XmlDataSource ID="XmlDataSource1" runat="server" 
        DataFile="~/Data/teams1-main.xml" XPath="round/team">
    </asp:XmlDataSource>

Open in new window

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 3

Expert Comment

by:AbdulQuddos
ID: 35165672
try the Solution# 1

Bind the DataSet to Repeator and use <%#Eval("FiledName")%> in templetes instead XPath
0
 

Author Comment

by:vstack
ID: 35166432
It dosn't work.  If I use <%#Eval("home")%> instead of ><%#XPath("home")%> I get nothing.  It has something to do with either the structure of the xml file or the xpath?

Vince
0
 

Author Comment

by:vstack
ID: 35166457
I tried Solution #1 but if I write <%#Eval("name")%>, I get 'main' and that's it, nothing else.

0
 
LVL 11

Expert Comment

by:SAMIR BHOGAYTA
ID: 35167920
Hi, your solution is here

<asp:Repeater ID="Repeater1" runat="server">
   <HeaderTemplate >TitleheaderTemplate>
   <ItemTemplate ><%# Eval("title") %>ItemTemplate>      
   <SeparatorTemplate ><hr />SeparatorTemplate>
<asp:Repeater>

ds = New DataSet
ds.ReadXml(Server.MapPath("test.xml"))
Repeater1.DataSource = ds
Repeater1.DataBind()

the other example is on this link
http://www.codeguru.com/csharp/csharp/cs_data/xml/article.php/c7183
0
 
LVL 3

Accepted Solution

by:
AbdulQuddos earned 2000 total points
ID: 35168007
Please find the attachment its a working sample Gridview + XML.

Hope this helps.
QuoteWidget.zip
0
 

Author Comment

by:vstack
ID: 35171283
Hi guys,

Thanks for the help so far.  My problem, as I have discovered, is that my .xml file is nested.  I cannot get at the member node in the file.  My xml looks like this:

 <team name="QU" ident="3" pullups="0">
    <home>QATAR</home>
    <positions></positions>
    <member name="Queenie">
      <points>0</points>
      <rounds>0</rounds>
    </member>
    <member name="Quinten">
      <points>0</points>
      <rounds>0</rounds>
    </member>
  </team>


If I remove nodes:

<home>QATAR</home>
    <positions></positions>

I can get to member but so far I have not been able to do it.  I have attached my code which gives the following error:

Object reference not set to an instance of an object.
Description: An unhandled exception occurred during the execution of the current web request. Please review the stack trace for more information about the error and where it originated in the code.

Exception Details: System.NullReferenceException: Object reference not set to an instance of an object.

Source Error:


Line 27:           <itemTemplate>
Line 28:              <i>
Line 29:       Speaker: <asp:Label ID="LabelSpeaker" runat="server" Text='<%# (CType(Container.DataItem, System.Xml.XmlNode).Attributes("name").Value)%>'></asp:Label>
Line 30:
Line 31:          
 
Again, if I take out the home and position nodes, it works.  I am not getting something mportant about xml?


Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load
        If Not Page.IsPostBack Then
            Dim doc As New XmlDocument()
            doc.Load(Server.MapPath("Data/teams1-main.xml"))
            Dim nodes As XmlNodeList = doc.SelectNodes("round/team")
            rpMyRepeater.DataSource = nodes
            rpMyRepeater.DataBind()
        End If

    End Sub

aspx page:

<asp:repeater id="rpMyRepeater" runat="server">
<HeaderTemplate>
   <table border="0">
</HeaderTemplate>
   <ItemTemplate>
   <tr>
      <td>
       Team:
          <asp:Label ID="LabelTeam" runat="server" Text='<%# (CType(Container.DataItem, System.Xml.XmlNode).Attributes("name").Value)%>'></asp:Label>&nbsp;
         Ident:
          <asp:Label ID="LabelIdent" runat="server" Text='<%# (CType(Container.DataItem, System.Xml.XmlNode).Attributes("ident").Value)%>'></asp:Label>
       
         <br />

          <asp:Repeater ID="Repeater2" runat="server" EnableViewState="false" DataSource= '<%# Container.DataItem %>' >
          <itemTemplate>
             <i>
      Speaker: <asp:Label ID="LabelSpeaker" runat="server" Text='<%# (CType(Container.DataItem, System.Xml.XmlNode).Attributes("name").Value)%>'></asp:Label>

           
          </i><br />
          </itemTemplate>
        </asp:Repeater>
      </td>
   </tr>
   </ItemTemplate>
   <FooterTemplate>
      </table>
   </FooterTemplate>
</asp:repeater>

Open in new window

0
 

Author Closing Comment

by:vstack
ID: 35908586
Thank you.  Sorry it's taken so long to return. Personal issues.
0

Featured Post

Free Tool: Site Down Detector

Helpful to verify reports of your own downtime, or to double check a downed website you are trying to access.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

Just a quick little trick I learned recently.  Now that I'm using jQuery with abandon in my asp.net applications, I have grown tired of the following syntax:      (CODE) I suppose it just offends my sense of decency to put inline VBScript on a…
This article discusses the ASP.NET AJAX ModalPopupExtender control. In this article we will show how to use the ModalPopupExtender control, how to display/show/call the ASP.NET AJAX ModalPopupExtender control from javascript, how to show/display/cal…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …
In this video, Percona Solution Engineer Rick Golba discuss how (and why) you implement high availability in a database environment. To discuss how Percona Consulting can help with your design and architecture needs for your database and infrastr…
Suggested Courses
Course of the Month14 days, 1 hour left to enroll

800 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