问题
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