简体   繁体   English

从XML文件读入数据集

[英]Reading from XML file into a DataSet

I try to fill a DataSet with values from a XML file like this so I can then fill a DataGridView with the values: 我尝试用这样的XML文件中的值填充DataSet ,以便随后用值填充DataGridView

DataSet ds = new DataSet();
ds.ReadXml(@"C:\aaa.xml");
dataGridView1.DataSource = ds;
dataGridView1.DataSource = "Products";

But I don't get anything. 但是我什么都没有。 What am I doing wrong? 我究竟做错了什么?

Are you sure your data loaded will be called Products in your data set?? 您确定加载的数据将在数据集中称为“ Products吗?

Try to verify by inspecting the tables in the dataset after you've loaded the data: 加载数据后,尝试通过检查数据集中的表进行验证:

DataSet ds = new DataSet();
ds. ReadXml(@"C:\aaa.xml");

foreach(DataTable t in ds.Tables)
{
   string tableName = t.TableName;   // put a breakpoint here - inspect the table names
}

If you want to use simply show the first table loaded, try this snippet: 如果要仅使用显示已加载的第一个表,请尝试以下代码段:

DataSet ds = new DataSet();
ds. ReadXml(@"C:\aaa.xml");

dataGridView1.DataSource = ds;
dataGridView1.DataMember = ds.Tables[0].TableName;
DataSet ds = new DataSet();
ds.ReadXml(@"C:\aaa.xml");
dataGridView1.DataSource = ds;
dataGridView1.Datamember= "Products";

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM