如何简单的用DataSet读取XML 并把它绑定到控件(GridView、DataList)
一共用到三个文件 XMLFile1.xml (xml数据文件) 、XmlTest.aspx(前台美工文件)、XmlTest.aspx.cs(后台程序文件)
1、先看 XMLFile1.xml (xml数据文件) 如下:
<?xml version="1.0" encoding="GB2312" ?>
<Root>
<Item>
<BlackID>123</BlackID>
<UserName>txl</UserName>
<BlackUserID>9364</BlackUserID>
<BlackUserName>usertxl</BlackUserName>
<CreateTime>2008-09-19</CreateTime>
</Item>
<Result>
<return_result>008678</return_result>
<Num>128</Num>
</Result>
</Root>
2、XmlTest.aspx(前台美工文件)如下:
<body>
<form id="form1" runat="server">
<div>
<asp:GridView ID="GridView1" runat="server">
</asp:GridView>
</div>
</form>
</body>
3、XmlTest.aspx.cs(后台程序文件)
protected void Page_Load(object sender, EventArgs e)
{
DataSet objDataSet = new DataSet();
objDataSet.ReadXml(Server.MapPath("XMLFile1.xml"));
GridView1.DataSource = objDataSet.Tables["Item"].Copy();
GridView1.DataBind();
}
得到如下结果:
BlackID UserName BlackUserID BlackUserName CreateTime
123 txl 9364 usertxl 2008-09-19
文章出处:http://www.diybl.com/course/4_webprogram/xml/xmlxl/2008920/144078.html