Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/asp.net/30.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 如何在ASP中将XML绑定到页面?_C#_Asp.net_Xml - Fatal编程技术网

C# 如何在ASP中将XML绑定到页面?

C# 如何在ASP中将XML绑定到页面?,c#,asp.net,xml,C#,Asp.net,Xml,我有这样一些XML: <?xml version="1.0" encoding="utf-8" ?> <People> <Person id="1"> <Name> <FirstName>Manoj</FirstName> <LastName>Syamala</LastName> </Name&g

我有这样一些XML:

    <?xml version="1.0" encoding="utf-8" ?>
    <People>
      <Person id="1">
        <Name>
          <FirstName>Manoj</FirstName>
          <LastName>Syamala</LastName>
        </Name>
      </Person>
      <Person id="2">
        <Name>
          <FirstName>Anthony</FirstName>
          <LastName>Roberts</LastName>
        </Name>
      </Person>
   </People>

马诺伊
西亚马拉
安东尼
罗伯茨
我希望能够在C#ASPX页面上显示这一点。 最好的办法是什么? 我想在一页上显示
Person
ID=1
,然后按下下一步按钮,然后用
Person
ID=2
替换该数据。我已经研究了使用ASP进行XML绑定的基础知识,但在
TreeNodeBinding
之前没有做过多少工作(这不是我需要的)


谢谢。

有很多方法可以做到这一点。但是,我建议您在分页时使用
DataGrid
。将您的
XML
作为
数据源
。等待示例代码

好的,这是代码

在您的aspx页面中创建一个
GridView
,如下所示。请注意,此GridView有一个
模板字段
,这样您就可以使用
HTML
表格式化数据

 <asp:GridView ID="gvPeople" AllowPaging="True" runat="server" 
            AutoGenerateColumns="False" PageSize="1" 
            onpageindexchanging="gvPeople_PageIndexChanging">
        <Columns>
            <asp:TemplateField>                
                <HeaderTemplate>
                    <table border="0">
                        <tr>
                </HeaderTemplate>
                <ItemTemplate>
                    <td>Name: </td><td><%# Eval("Name.FullName") %></td>
                </ItemTemplate>
                <FooterTemplate>
                    </tr>
                  </table>
                </FooterTemplate>
            </asp:TemplateField>
        </Columns>
    </asp:GridView>
下面是用于创建保存XML数据的对象的支持类

using System;
using System.Collections.Generic;

namespace ReadXMLData
{
    [Serializable]
    [System.Xml.Serialization.XmlRoot("People")]
    public class People : List<Person>
    {
    }

    [Serializable]
    public class Person
    {
        public Name Name { get; set; }

        public Person()
        {
            this.Name = new Name();
        }
    }

    [Serializable]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.LastName;
            }
        }

        public Name()
        {
            this.FirstName = string.Empty;
            this.LastName = string.Empty;
        }
    }
}
使用系统;
使用System.Collections.Generic;
名称空间ReadXMLData
{
[可序列化]
[System.Xml.Serialization.XmlRoot(“人”)]
公共阶层人士:名单
{
}
[可序列化]
公共阶层人士
{
公共名称名称{get;set;}
公众人士()
{
this.Name=新名称();
}
}
[可序列化]
公共类名
{
公共字符串名{get;set;}
公共字符串LastName{get;set;}
公共字符串全名
{
得到
{
返回this.FirstName+“”+this.LastName;
}
}
公共名称()
{
this.FirstName=string.Empty;
this.LastName=string.Empty;
}
}
}

希望这有帮助。

Hi@Mike,这些答案对你有帮助吗?如果是,请你接受并投票好吗?通过这种方式,您将鼓励其他开发人员参与这些问答。谢谢!
using System;
using System.Collections.Generic;

namespace ReadXMLData
{
    [Serializable]
    [System.Xml.Serialization.XmlRoot("People")]
    public class People : List<Person>
    {
    }

    [Serializable]
    public class Person
    {
        public Name Name { get; set; }

        public Person()
        {
            this.Name = new Name();
        }
    }

    [Serializable]
    public class Name
    {
        public string FirstName { get; set; }
        public string LastName { get; set; }
        public string FullName
        {
            get
            {
                return this.FirstName + " " + this.LastName;
            }
        }

        public Name()
        {
            this.FirstName = string.Empty;
            this.LastName = string.Empty;
        }
    }
}