Skip to main content
 首页 » 编程设计

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

2025年05月04日43youxin

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