gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-12-03 17:21:05 25 4
gpt4 key购买 nike

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

关于c# - 没有属性的C#XmlDocument SelectSingleNode,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57905933/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com