且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何将XML文件中的数据绑定到GridView

更新时间:2023-10-06 15:01:04

解析将在数据集中创建2个数据表。一个用于学生,一个用于导师。

如果你想将导师绑定到gridview,你可以通过以下方式之一进行。



The parsing will create 2 datatables in the dataset. One for Students and one for Tutors.
If you want to bind the Tutors to the gridview, you can do it on one of the following ways.

using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/StudentsSystem.xml"));
        GridView1.DataSource = ds.Tables[1];//Tutors is the second table and it is zero based index
        GridView1.DataBind();
    }






OR

using (DataSet ds = new DataSet())
    {
        ds.ReadXml(Server.MapPath("~/StudentsSystem.xml"));
        GridView1.DataSource = ds.Tables["Tutors"];
        GridView1.DataBind();
    }





我建议使用第二个,因为使用表名很容易识别。



I would suggest the second one as it is easy to identify using table names.