我需要获取缺少特定参数的xml节点。假设我有以下c:\ temp \ a.xml:
<files>
<file product="myproduct">C:\file_myproduct</file>
<file>C:\file_general</file>
</files>
如何获取没有属性的C:\ file_general值?我试过了:
var doc = new XmlDocument();
doc.Load(@"c:\temp\a.xml");
// C:\file_myproduct - good
string myproduct = doc.SelectSingleNode("/files/file[@product='myproduct']").InnerText;
// I need C:\file_general here, but this gives again the C:\file_myproduct
string general = doc.SelectSingleNode("/files/file").InnerText;
最佳答案
您可以通过使用not(...)函数来实现:
string general = doc.SelectSingleNode("/files/file[not(@product)]").InnerText;
它指定为here, in the W3C Recommendation "XML Path Language (XPath) Version 1.0",在.NET System.Xml.XmlDocument中实现。
关于c# - 没有属性的C#XmlDocument SelectSingleNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57905933/