【问题标题】:C# XmlDocument SelectSingleNode without attributeC# XmlDocument SelectSingleNode 没有属性
【发布时间】:2019-09-12 11:39:42
【问题描述】:

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

【问题讨论】:

    标签: c# xpath


    【解决方案1】:

    您可以通过使用 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。

    【讨论】:

    • 虽然此代码可能会回答问题,但提供有关此代码为何和/或如何回答问题的额外上下文可提高其长期价值
    猜你喜欢
    • 1970-01-01
    • 2020-05-30
    • 2011-02-07
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2015-06-18
    • 1970-01-01
    相关资源
    最近更新 更多