我通过以下代码读出了websites.xml中的所有数据:
DataSet ds = new DataSet();
ds.ReadXml("websites.xml");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;websites.xml:
<?xml version="1.0" encoding="gb2312"?>
<websites>
<web name="谷歌" url="http://www.g.cn">
<title>Google</title>
<type>Search</type>
<star>5</star>
</web>
<web name="百度" url="http://www.baidu.com">
<title>Baidu</title>
<type>Search</type>
<star>5</star>
</web>
<web name="新浪" url="http://www.sina.com">
<title>Sina</title>
<type>Portal</type>
<star>4</star>
</web>
<web name="CCTV" url="http://www.cctv.com">
<title>CCTV</title>
<type>Media</type>
<star>3</star>
</web>
<web name="腾讯" url="http://www.QQ.com">
<title>QQ</title>
<type>Portal</type>
<star>4</star>
</web>
</websites>但是我只想读出xml中"type=Search"的数据,并绑定到datagridview中,请问该如何办到?即如何读取xml中的指定值绑定到datagridview?(麻烦给出详细的实现代码。)
DataSet ds = new DataSet();
ds.ReadXml("websites.xml");
this.dataGridView1.DataSource = ds.Tables[0].DefaultView;websites.xml:
<?xml version="1.0" encoding="gb2312"?>
<websites>
<web name="谷歌" url="http://www.g.cn">
<title>Google</title>
<type>Search</type>
<star>5</star>
</web>
<web name="百度" url="http://www.baidu.com">
<title>Baidu</title>
<type>Search</type>
<star>5</star>
</web>
<web name="新浪" url="http://www.sina.com">
<title>Sina</title>
<type>Portal</type>
<star>4</star>
</web>
<web name="CCTV" url="http://www.cctv.com">
<title>CCTV</title>
<type>Media</type>
<star>3</star>
</web>
<web name="腾讯" url="http://www.QQ.com">
<title>QQ</title>
<type>Portal</type>
<star>4</star>
</web>
</websites>但是我只想读出xml中"type=Search"的数据,并绑定到datagridview中,请问该如何办到?即如何读取xml中的指定值绑定到datagridview?(麻烦给出详细的实现代码。)
再声明白一个 bindingsource对像 里面有一个filter的属性,设置成type=Search 就可以了..
那东西相当的于sql语句里面的where....
select就可以了
刚才在MSDN查了一下,http://msdn.microsoft.com/zh-cn/library/system.windows.forms.bindingsource.filter(VS.80).aspx。。
知道该怎么用。。一下给出代码,供其他人参考:
DataSet ds = new DataSet();
ds.ReadXml("websites.xml");
BindingSource bs = new BindingSource();
bs.DataSource = ds.Tables[0];
bs.Filter = "type='Search'";
this.dataGridView1.DataSource = bs;谢谢勒。。