如何在winform中的datagridview中显示xml文件数据

时间:2015-02-20 07:05:58

标签: c# xml gridview

我有像

这样的xml文件
<?xml version="1.0" encoding="UTF-8"?>
<CONSOLIDATED_LIST dateGenerated="2015-01-23T00:00:00">
  <INDIVIDUALS>
    <INDIVIDUAL> 
      <DataId>1</DataId> 
      <Name>Hitesh</Name> 
      <Address>Mumbai</Address> 
    </INDIVIDUAL>
    <INDIVIDUAL> 
      <DataId>2</DataId> 
      <Name>Jitesh</Name> 
      <Address>Mumbai</Address> 
    </INDIVIDUAL>
    <INDIVIDUAL> 
      <DataId>3</DataId> 
      <Name>Ritesh</Name> 
      <Address>Mumbai</Address> 
    </INDIVIDUAL>
    <INDIVIDUAL> 
      <DataId>4</DataId> 
      <Name>Mitesh</Name> 
      <Address>Mumbai</Address> 
    </INDIVIDUAL>
    <INDIVIDUAL> 
      <DataId>5</DataId> 
      <Name>Nitesh</Name> 
      <Address>Mumbai</Address> 
    </INDIVIDUAL> 
  </INDIVIDUALS
  <ENTITY>
    <ENTITIES> 
      <DataId>11</DataId>
      <Name>Hitesh</Name> 
      <Address>Mumbai</Address> 
    </ENTITIES>
    <ENTITIES> 
      <DataId>12</DataId> 
      <Name>Hitesh</Name> 
      <Address>Mumbai</Address> 
    </ENTITIES>
    <ENTITIES> 
      <DataId>13</DataId> 
      <Name>Hitesh</Name>
      <Address>Mumbai</Address> 
     </ENTITIES> 
   </ENTITY> 
 </CONSOLIDATED_LIST>

我的代码是

openFileDialog1.Multiselect = true;
openFileDialog1.InitialDirectory = Environment.GetFolderPath(Environment.SpecialFolder.Desktop);
txtXML.Text = Path.GetFileNameWithoutExtension(openFileDialog1.FileName);
txtXML.Font = new Font(txtXML.Font, FontStyle.Bold);
DataSet ds = new DataSet();
ds.ReadXml(openFileDialog1.FileName);


**dataGridView1.DataSource = ds.Tables[0];**  ------Error (Only shows First table i.e. <CONSOLIDATED_LIST dateGenerated="2015-01-23T00:00:00">)
ds.Dispose();

当我把这个

dataGridView1.DataSource = ds.Tables[2];

它只显示INDIVIDUAL标签数据,而不显示ENTITY数据。

0 个答案:

没有答案