xml文件
<?xml version="1.0" encoding="utf-8"?>
<ZZRK>
<person>
<xm>吴</xm>
<bmch>无意</bmch>
<xb>1</xb>
<mz>17</mz>
<sfzh>456</sfzh>
</person>
<person>
<xm>吴</xm>
<bmch>无意</bmch>
<xb>1</xb>
<mz>17</mz>
<sfzh>456</sfzh>
</person>
<person>
<xm>111</xm>
<bmch>
</bmch>
<xb>0</xb>
<mz>01</mz>
<sfzh>1111111111111</sfzh>
</person>
<person>
<id>20100901049i31</id>
<xm>123</xm>
<bmch>
</bmch>
<xb>0</xb>
<mz>01</mz>
<sfzh>1111111111111</sfzh>
</person>
</ZZRK>
使用
StringReader read = new StringReader(objXmlDoc.SelectSingleNode(@"/ZZRK/person[contains(xm,吴)]").OuterXml);
ds.ReadXml(read);
为什么 dataset 中只有一条数据啊?
类似的查询也是一样的,只能出来一条数据
如何解决
SelectSingleNode……Single……
static void Main(string[] args)
{
GetXMLDate(Environment.CurrentDirectory + "\\demo2010.xml", "xm","吴");
Console.ReadKey();
}
private static void GetXMLDate(string xmlPath,string column, string filter)
{
DataSet dataSet = new DataSet();
dataSet.ReadXml(xmlPath);
DataRow[] rows = (dataSet.Tables[0].Select(string.Format(" {0}='{1}'",column,filter)));
foreach (DataRow dr in rows)
{
Console.WriteLine(dr["bmch"]);
}
}
Demo2010.xml内容:
<?xml version="1.0" encoding="utf-8" ?>
<ZZRK>
<person>
<xm>吴</xm>
<bmch>无意</bmch>
<xb>1</xb>
<mz>17</mz>
<sfzh>456</sfzh>
</person>
<person>
<xm>吴</xm>
<bmch>无意</bmch>
<xb>1</xb>
<mz>17</mz>
<sfzh>456</sfzh>
</person>
<person>
<xm>111</xm>
<bmch>
</bmch>
<xb>0</xb>
<mz>01</mz>
<sfzh>1111111111111</sfzh>
</person>
<person>
<id>20100901049i31</id>
<xm>123</xm>
<bmch>
</bmch>
<xb>0</xb>
<mz>01</mz>
<sfzh>1111111111111</sfzh>
</person>
</ZZRK>
SelectSingleNode当然只选择一个节点
二楼有源码,呵呵。