c# - 在 .NET 2 中搜索具有特定属性值的节点的 XML 文件

标签 c# xpath .net-2.0

我找到了使用 LINQ 搜索 XML 节点的答案,但我仅限于使用 C# 和 .NET 2。

我想打开一个 XML 文件(~50Kb,所有简单文本)并搜索所有 <Tool>属性为 name 的节点具有特定的值(value)。

好像XmlDocument.SelectNodes()可能是我要找的,但我不知道 XPath。这是正确的方法吗?如果是这样,代码会是什么样子?

最佳答案

您可以在 XmlDocument.SelectNodes 中使用 XPath,例如:SelectNodes("//ElementName[@AttributeName='AttributeValue']")

XML 示例:

<root>
    <element name="value1" />
    <element name="value2" />
    <element name="value1" />
</root>

C# 示例:

XmlDocument xDoc = new XmlDocument();
// Load Xml

XmlNodeList nodes = xDoc.SelectNodes("//element[@name='value1']");
// nodes.Count == 2

Here你可以找到一些额外的 XPath 示例

关于c# - 在 .NET 2 中搜索具有特定属性值的节点的 XML 文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14501038/

相关文章:

c# - 滚动到底部,但内容保持在顶部

xpath - Scrapy + Splash:在内部html内抓取元素

python - 无法从一些复杂的 html 元素中获取特定内容

c# - .net 3.5 中的 WebFaultException 类

c# - Visual C# Directory.GetDirectories 问题 - "The specified server cannot perform the requested operation"

Selenium - 设置切换

c# - WinForms ListView.HideSelection 属性没有效果?

c# - 设置 gridView.FirstDisplayedScrollingRowIndex 时获取 "No room is available to display rows"

c# - SortedDictionary 的性能与对字典进行排序

c# - 获取Button中textblock的值