C# XmlDocument SelectSingleNode without attribute

♀尐吖头ヾ 提交于 2021-02-11 10:28:07

问题


I need to get xml node which misses specific argument. Let's say I have following c:\temp\a.xml:

<files>
  <file product="myproduct">C:\file_myproduct</file>
  <file>C:\file_general</file>
</files>

How can I get the C:\file_general value which has no attribute? I tried:

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;

回答1:


You can achieve this by using the not(...) function:

string general = doc.SelectSingleNode("/files/file[not(@product)]").InnerText;

It is specified here, in the W3C Recommendation "XML Path Language (XPath) Version 1.0", which is implemented in .NET, System.Xml.XmlDocument.



来源:https://stackoverflow.com/questions/57905933/c-sharp-xmldocument-selectsinglenode-without-attribute

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!