如何将xml文件绑定到Gridview

时间:2011-06-28 11:00:32

标签: c# asp.net xml

我有Xml文件..我想将这个xml数据绑定到gridview ..

我的档案:

<ExtPageList>
  <version>E36M3P6</version>
  <resultCode>0</resultCode>
  <resultStr>Success</resultStr>
  <objects>
    <ProcessStatus process="monitor">
      <id>6062</id>
      <condition>running</condition>
      <runLevel>7</runLevel>
      <state>sleeping</state>
      <starts>1</starts>
      <uptime>P3DT13H2M37S</uptime>
      <fds>21</fds>
    </ProcessStatus>
  </objects>
  <objects>
    <ProcessStatus process="manager">
      <id>6301</id>
      <condition>running</condition>
      <runLevel>7</runLevel>
      <state>sleeping</state>
      <starts>1</starts>
      <uptime>P3DT13H2M37S</uptime>
      <fds>57</fds>
    </ProcessStatus>
  </objects>
  <totalPages>1</totalPages>
  <currentPage>1</currentPage>
  <pageSize>19</pageSize>
</ExtPageList>

我希望将此数据显示为包含所有数据的此格式..

进程ID条件状态

6 个答案:

答案 0 :(得分:3)

最简单的方法是使用数据集的ReadXml()

DataSet xmlData = new DataSet();
xmlData.ReadXml("D:\\books.xml");
gridControl1.DataSource = xmlData.Tables[0];

答案 1 :(得分:2)

绑定xml文件并在网格视图中显示xml文件的数据。这是

的代码
<script runat="server">
    void Page_Load(Object sender, EventArgs e)
    {
        DataSet authorsDataSet;
        string filePath = Server.MapPath("Authors.xml");
        authorsDataSet = new DataSet();
        //Read the contents of the XML file into the DataSet
        authorsDataSet.ReadXml(filePath);                    
        authorsGird.DataSource = authorsDataSet.Tables[0].DefaultView;
        authorsGird.DataBind();
    }

</script>

下面是网格视图的html标记

<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
    <title>Reading XML Data into a DataSet object </title>
</head>
<body>
    <form id="form1" runat="server">
    <div>
        <asp:GridView id="authorsGird" runat="server" 
            AutoGenerateColumns="False" CellPadding="4" HeaderStyle-BackColor="blue" HeaderStyle-ForeColor="White" 
            HeaderStyle-HorizontalAlign="Center" HeaderStyle-Font-Bold="True">
            <Columns>
                <asp:BoundField HeaderText="Last Name" DataField="lastName" />
                <asp:BoundField HeaderText="First Name" 
                    DataField="firstName" ItemStyle-HorizontalAlign="Right" />
            </Columns>           
        </asp:GridView>
    </div>
    </form>
</body>
</html>

答案 2 :(得分:2)

检查详细信息:How To Read XML Data into a DataSet by Using Visual C# .NET

    string myXMLfile = @"C:\MySchema.xml";
    DataSet ds = new DataSet();
    // Create new FileStream with which to read the schema.
    System.IO.FileStream fsReadXml = new System.IO.FileStream 
        (myXMLfile, System.IO.FileMode.Open);
    try
    {
        ds.ReadXml(fsReadXml);
        dataGrid1.DataSource = ds.Tables[0];
        //dataGrid1.DataMember = "Cust";
        dataGrid1.DataBind();
    }
    catch (Exception ex)
    {

    }
    finally
    {
    fsReadXml.Close();
    }

答案 3 :(得分:1)

      DataSet ds1 = new DataSet();//this dataset to read XML to datagrid
      ds1.ReadXml(Server.MapPath(”xmlData.xml”));
      gridview.DataSource = ds1.Tables[0].DefaultView;
      gridview.DataBind();

答案 4 :(得分:0)

所有已发布的答案都应该有效;我认为您的问题是数据集将您的XML视为两个表(而不是一个)。尝试将第二个表绑定到gridview,如下所示:

  gridview.DataSource = ds1.Tables[1].DefaultView;

答案 5 :(得分:0)

Dataset ds_resp=new Dataset()
ds_resp.ReadXml(Server.MapPath("xmlfiles/PPForms.xml"));
gridview.DataSource = ds_resp;
gridview.DataBind();