c# - 没有属性的C#XmlDocument SelectSingleNode

标签 c# xpath

我需要获取缺少特定参数的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/

相关文章:

c# - 如何向 Linq 表达式添加排序规则?

c# - 如何确定字符串是希伯来日期还是公历日期?

c# - 删除目录中除 C# 中的文件之外的所有内容

java - VTD XML 内部 XPath 表达式

xml - 无法在 import.io 的爬虫中为网页元素获取正确的 XPath

c# - 我仍然可以使用 CAML 进行此查询吗?

c# - 在属性网格上显示数据

java - 从 tesseract hOCR XML 输出中选择的 XPathExpression

python - Selenium/python 2.7 如何查找只有标题的元素?

python - Scrapy 迭代同级节点 - xpath 产生空列表