我想从xml文档中提取某些值。在下面的示例中,我想将存储在'c'和'd'节点中的值存储在列表中,但只有'b'节点同时包含'c'和'd'的情况。我到目前为止的代码循环遍历所有'b'节点,但我不确定在while循环中放置什么,或者如果这是最好的方法。当两个子节点必须存在时,如何使用XPath从XML文件中提取子节点?
XmlDocument attrsXML = new XmlDocument();
attrsXML.LoadXml(dbReader["SampleXml"].ToString());
XPathNavigator nav = attrsXML.CreateNavigator();
XPathNodeIterator attribNodes = nav.Select("https://stackoverflow.com/a/b");
while (attribNodes.MoveNext())
{
// What do I need to put here in order to extract the 'c' and 'd' nodes?
// Any other nodes can be ignored (such as 'e' above). I am only interested
// when 'b' contains both 'c' AND 'd'.
}
其中 'SampleXml' 从数据库加载是:
<a>
<b>
<c>Extract this</c>
<d>And this</d>
<e>not this</e>
</b>
<b>
<c>not this</c>
<e>not this</e>
</b>
<b>
<c>Extract this</c>
<d>And this</d>
</b>
</a>
任何帮助表示赞赏。
谢谢eminsenay - 我已更改为使用您的解决方案。比我的好多了! – psych