如何将数据从XML文件绑定到网格视图

时间:2018-10-28 06:54:16

标签: asp.net xml gridview data-binding

我目前遇到这个问题,我试图将数据从xml文件绑定到网格视图。我搜索了网络,找到了与我类似的问题的解决方案,但是由于某些原因它们没有起作用。 我的以下代码是

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>k.bernal@gmail.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</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();
    }

这类似于我发现的解决方案,但问题是未声明gvRegistrations,但我不知道应该在哪里或如何声明它。我知道我丢失了一些东西,只是不确定什么...

1 个答案:

答案 0 :(得分:1)

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

   <registrations>
      <Registration>
         <id>1</id>
         <fullName>Keiran Bernal</fullName>
         <emailAddress>k.bernal@gmail.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>2</id>
         <fullName>Cordelia Pierce</fullName>
         <emailAddress>c.pierce@outlook.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>3</id>
         <fullName>Zachery Guy</fullName>
         <emailAddress>z.guy@yahoo.com</emailAddress>
         <registrationType>conference only</registrationType>
         <attendingSocialEvent>yes</attendingSocialEvent>
      </Registration>
      <Registration>
         <id>4</id>
         <fullName>Kiana Hawworth</fullName>
         <emailAddress>k.hawworth@bigpond.com</emailAddress>
         <registrationType>conference and Dinner</registrationType>
         <attendingSocialEvent>no</attendingSocialEvent>
      </Registration>
   </registrations>

感谢@VDWWD,在这里进行了更改后,它仍然有效