我有一个xml文件的片段,如下所示:
<?xml version="1.0" encoding="utf-8" standalone="yes"?>
<?xml-stylesheet type="text/xsl" href="wa_xml2html.xsl"?>
<computeraudit>
<title>Computer Audit :: 11/13/2012 10:43:22 AM</title>
<category title="Loaded Modules">
<subcategory title="">
<recordset title="">
<fieldname>Name</fieldname>
<fieldname>Version</fieldname>
<fieldname>Modified</fieldname>
<fieldname>Manufacturer</fieldname>
<datarow>
<fieldvalue>XmlLite.dll</fieldvalue>
<fieldvalue>1.3.1000.0</fieldvalue>
<fieldvalue>7/13/2009 8:16:21 PM</fieldvalue>
<fieldvalue>Microsoft Corporation</fieldvalue>
</datarow>
<datarow>
<fieldvalue>zip.dll</fieldvalue>
<fieldvalue>6.0.250.6</fieldvalue>
<fieldvalue>5/25/2011 8:30:12 AM</fieldvalue>
<fieldvalue>Sun Microsystems, Inc.</fieldvalue>
</datarow>
</recordset>
</subcategory>
</category>
</computeraudit>
我正在尝试使用C#XPath解析它并获取每个fieldvalue元素值。
c#代码如下所示:
XPathNavigator nav;
XPathDocument docNav;
XPathNodeIterator NodeIter;
String strExpression;
docNav = new XPathDocument(@"C:\TEMP\WinAudit\test.xml");
nav = docNav.CreateNavigator();
strExpression = "/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow";
NodeIter = nav.Select(strExpression);
while (NodeIter.MoveNext())
{
Console.WriteLine(NodeIter.Current.Value);
}
Console.ReadLine();
它为每个数据行节点返回一行,并将所有fieldvalue元素值连接在一起。
如何返回每个fieldvalue元素的每个不同值?
答案 0 :(得分:1)
您的选择器仅选择数据行元素。
尝试strExpression = "/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow/fieldvalue";
答案 1 :(得分:1)
将您的XPath表达式更改为:
"/computeraudit/category[@title=\"Loaded Modules\"]/subcategory/recordset/datarow/fieldvalue"