Link to home
Start Free TrialLog in
Avatar of Vincent Stack
Vincent StackFlag for Qatar

asked on

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

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>
Avatar of AbdulQuddos
AbdulQuddos
Flag of Pakistan image

.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
Avatar of Vincent Stack

ASKER

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

try the Solution# 1

Bind the DataSet to Repeator and use <%#Eval("FiledName")%> in templetes instead XPath
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
I tried Solution #1 but if I write <%#Eval("name")%>, I get 'main' and that's it, nothing else.

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
ASKER CERTIFIED SOLUTION
Avatar of AbdulQuddos
AbdulQuddos
Flag of Pakistan image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
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

Thank you.  Sorry it's taken so long to return. Personal issues.