?
Solved

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

Posted on 2011-03-18
10
Medium Priority
?
464 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
  • 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
What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

 
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

Technology Partners: 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!

Question has a verified solution.

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

Sometimes in DotNetNuke module development you want to swap controls within the same module definition.  In doing this DNN (somewhat annoyingly) swaps the Skin and Container definitions to the default admin selections.  To get around this you need t…
Problem Hi all,    While many today have fast Internet connection, there are many still who do not, or are connecting through devices with a slower connect, so light web pages and fast load times are still popular.    If your ASP.NET page …
Look below the covers at a subform control , and the form that is inside it. Explore properties and see how easy it is to aggregate, get statistics, and synchronize results for your data. A Microsoft Access subform is used to show relevant calcul…
Whether it be Exchange Server Crash Issues, Dirty Shutdown Errors or Failed to mount error, Stellar Phoenix Mailbox Exchange Recovery has always got your back. With the help of its easy to understand user interface and 3 simple steps recovery proced…
Suggested Courses
Course of the Month13 days, 12 hours left to enroll

755 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