我正在尝试在 c# 中使用 xpath 选择节点
这是我的 XML 文件
<?xml version="1.0" encoding="utf-8"?>
<xObject version="3.0.2002.0" xmlns="http://schemas.microsoft.com/wix/2006/objects">
<section id="*" type="product">
<table name="NotThis">
<row sourceLineNumber="D:\bla\bla\">
<field>Borderish.fo</field>
<field>Documents</field>
<field>1</field>
<field>No, not this line here 1</field>
</row>
<row sourceLineNumber="D:\blah\blah\">
<field>Charterish</field>
<field>Documents</field>
<field>1</field>
<field>No not, this line here 2</field>
</row>
</table>
<table name="XFile">
<row sourceLineNumber="D:\bla\bla\">
<field>Borderish.fo</field>
<field>Documents</field>
<field>1</field>
<field>This line here 1</field>
</row>
<row sourceLineNumber="D:\blah\blah\">
<field>Charterish</field>
<field>Documents</field>
<field>1</field>
<field>This line here 2</field>
</row>
</table>
</section>
</xObject>
这是我的 C# 代码,它似乎不起作用
XmlDocument doc = new XmlDocument();
doc.Load("Testing.xml");
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("ns", "xmlns="http://schemas.microsoft.com/wix/2006/objects"");
XmlNodeList nodeList = root.SelectNodes("ns:table[@type='XFile']/ns:row", nsmgr);
foreach (XmlNode xn in nodeList)
{
string fieldLine = xn["Field"].InnerText;
Console.WriteLine("Field: {4}", fieldLine);
}
我要输出的是每 4 个字段表名 =“xfile”,如下所示:
This line here 1
This line here 2
如果您知道解决方案或更好的方法,请告诉我。