Solved

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

Posted on 2011-03-18
10
456 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
Master Your Team's Linux and Cloud Stack

Come see why top tech companies like Mailchimp and Media Temple use Linux Academy to build their employee training programs.

 
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 500 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

Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

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…
I have developed many web applications with asp & asp.net and to add and use a dropdownlist was always a very simple task, but with the new asp.net, setting the value is a bit tricky and its not similar to the old traditional method. So in this a…
This Micro Tutorial will teach you how to censor certain areas of your screen. The example in this video will show a little boy's face being blurred. This will be demonstrated using Adobe Premiere Pro CS6.
This tutorial gives a high-level tour of the interface of Marketo (a marketing automation tool to help businesses track and engage prospective customers and drive them to purchase). You will see the main areas including Marketing Activities, Design …

773 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