如何将XML文件中的数据绑定到GridView [英] How to bind data from XML file to GridView

查看:78
本文介绍了如何将XML文件中的数据绑定到GridView的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有这个XML文件结构:



I have this XML file structure:

<StudentsSystem>
   <Students StudentId="">
    <FirstName></FirstName>
    <LastName></LastName>
    <Gender></Gender>
    <BirthDate></BirthDate>
    <HomeCity></HomeCity>
    <Address></Address>
    <Class></Class>
    <StudiesYear></StudiesYear>
  </Students>

  <Tutors TutorID="">
    <FirstName></FirstName>
    <LastName></LastName>
    <NickName></NickName>
    <Gender></Gender>
    <BirthDate></BirthDate>
    <HomeCity></HomeCity>
    <Address></Address>
    <Mobile></Mobile>
    <Email></Email>
    <Class></Class>
    <Subject></Subject>
  </Tutors>
  

</StudentsSystem>





我希望将导师绑定到gridview,

我试过这个:



and I want to bind Tutors to gridview,
I tried this:

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





但这是错误的并获得整个XML文件..

我能做什么?写??



But this's wrong and get the whole XML file..
What could I write?!

推荐答案

解析将在数据集中创建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(&quot;~/StudentsSystem.xml&quot;));
        GridView1.DataSource = ds.Tables["Tutors"];
        GridView1.DataBind();
    }





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



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


这篇关于如何将XML文件中的数据绑定到GridView的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆