Solved

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

Posted on 2011-03-18
10
454 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
Comment Utility
.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
Comment Utility
Here is the second possible solution:
http://www.ezzylearning.com/tutorial.aspx?tid=8462553
0
 

Author Comment

by:vstack
Comment Utility
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
 
LVL 3

Expert Comment

by:AbdulQuddos
Comment Utility
try the Solution# 1

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

Author Comment

by:vstack
Comment Utility
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
What Security Threats Are You Missing?

Enhance your security with threat intelligence from the web. Get trending threat insights on hackers, exploits, and suspicious IP addresses delivered to your inbox with our free Cyber Daily.

 

Author Comment

by:vstack
Comment Utility
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
Comment Utility
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 500 total points
Comment Utility
Please find the attachment its a working sample Gridview + XML.

Hope this helps.
QuoteWidget.zip
0
 

Author Comment

by:vstack
Comment Utility
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
Comment Utility
Thank you.  Sorry it's taken so long to return. Personal issues.
0

Featured Post

6 Surprising Benefits of Threat Intelligence

All sorts of threat intelligence is available on the web. Intelligence you can learn from, and use to anticipate and prepare for future attacks.

Join & Write a Comment

In this Article, I will provide a few tips in problem and solution manner. Opening an ASPX page in Visual studio 2003 is very slow. To make it fast, please do follow below steps:   Open the Solution/Project. Right click the ASPX file to b…
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.
Sending a Secure fax is easy with eFax Corporate (http://www.enterprise.efax.com). First, Just open a new email message.  In the To field, type your recipient's fax number @efaxsend.com. You can even send a secure international fax — just include t…
In this seventh video of the Xpdf series, we discuss and demonstrate the PDFfonts utility, which lists all the fonts used in a PDF file. It does this via a command line interface, making it suitable for use in programs, scripts, batch files — any pl…

744 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

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now