Jason Crisp
Jason Crisp

Reputation: 43

How to Bind Data to a Grid View from an XML file

I currently have this issue where I am trying to bind data from an xml file to a grid view. I have searched the net and have found solutions for problems similar to mine however they have not worked for some reason. The following code I have is

Index.aspx

 <asp:GridView ID="gvRegistrations" runat="server" AutoGenerateColumns="false">
        <Columns>
            <%-- ID --%>
            <asp:TemplateField HeaderText="ID">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_ID" runat="server" Text='<%# Bind ("id")%>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Full Name--%>
            <asp:TemplateField HeaderText="Full Name">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_FullName" runat="server" Text='<%# Bind ("fullName") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Email--%>
            <asp:TemplateField HeaderText="Email">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_Email" runat="server" Text='<%# Bind("email") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Registration Type--%>
            <asp:TemplateField HeaderText="Registration Type">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_RegistrationType" runat="server" Text='<%# Bind ("registrationType") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
            <%--Attending Social Event--%>
               <asp:TemplateField HeaderText="Attending">
                <ItemTemplate>
                    <asp:Label ID="lblRegistrations_Attending" runat="server" Text='<%# Bind ("attendingSocialEvent") %>'></asp:Label>
                </ItemTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>  

Registrations.xml

<?xml version="1.0" encoding="UTF-8"?>
<RegistrationCollection>
   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
   </registrations>
</RegistrationCollection>

Index.aspx.cs

public partial class _Default : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        ds.ReadXml(Server.MapPath("~/registrations.xml"));
        gvRegistrations.DataSource = ds;
        gvRegistrations.DataBind();
    }

This is similar to soltutions I have found but the issue is gvRegistrations is not declared but I do not know where or how I should declare it. Iknow I am missing something just not sure what...

Upvotes: 1

Views: 168

Answers (1)

Jason Crisp
Jason Crisp

Reputation: 43

<?xml version="1.0" encoding="UTF-8"?>

   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>[email protected]</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
   </registrations>

Thanks to @VDWWD, after the changes made here it worked

Upvotes: 1

Related Questions