C# XmlDocument SelectSingleNode 没有属性 [英] C# XmlDocument SelectSingleNode without attribute

查看:43
本文介绍了C# XmlDocument SelectSingleNode 没有属性的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要获取缺少特定参数的 xml 节点.假设我有以下 c:\temp\a.xml:

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>

如何获取没有属性的 C:\file_general 值?我试过了:

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;

推荐答案

您可以使用 not(...) 函数来实现:

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

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

它在 此处指定,在 W3C 中推荐XML Path Language (XPath) Version 1.0",在.NET中实现,System.Xml.XmlDocument.

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

这篇关于C# XmlDocument SelectSingleNode 没有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆