使用列过滤将xml加载到数据集中

时间:2010-08-16 07:35:10

标签: c# xml winforms

我想将xml加载到只有2列(名称,价格)

的数据集中
<?xml version="1.0" encoding="utf-8" ?>
<file>
<record>
    <name>A</name>
    <address>B</address>
    <date>12-12-2010</date>
    <price>100</price>
</record>
<record>
    <name>B</name>
    <address>C</address>
    <date>01-01-1999</date>
    <price>23</price>
</record>
</file>

这是我的c#代码:

string myXMLfile = "C:\\asdf.xml";
DataSet ds = new DataSet();

ds.ReadXml(myXMLfile);

dataGrid1.DataSource = ds;
dataGrid1.DataMember = "record";

显示所有记录。我该怎么做才能过滤数据?

过滤数据集/ dataview / datatable

或读取特定的xml元素并将它们加载到数据集中?

或过滤datagrdview?

非常感谢

2 个答案:

答案 0 :(得分:1)

  1. dataGridView1.AutoGenerateColumns设为false。
  2. 仅显示必填列。

答案 1 :(得分:0)

如何使用LINQ to XML读取XML XElement.Load(@"C:\asdf.xml"); 然后使用LINQ过滤结果并调用ToList ...为什么需要使用数据集?

相关问题