在.NET 2中搜索XML文件以查找具有特定属性值的节点 [英] Search XML file for nodes with specific attribute value in .NET 2

查看:204
本文介绍了在.NET 2中搜索XML文件以查找具有特定属性值的节点的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I found answers for searching XML nodes using LINQ, but I am limited to C# with .NET 2.

我想打开一个XML文件(〜50Kb) (所有纯文本),然后搜索具有属性 name 具有特定值的所有< Tool> 节点。

I want to open a single XML file (~50Kb, all simple text) and search for all <Tool> nodes with attribute name having a specific value.

似乎是 XmlDocument.SelectNodes()可能是我要找的东西,但我不知道XPath 。

It seems like XmlDocument.SelectNodes() might be what I'm looking for, but I don't know XPath. Is this the right way and if so what would code look like?

推荐答案

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

You can use XPath in XmlDocument.SelectNodes such as: 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

在这里,您可以找到其他一些XPath示例

Here you can find some additional XPath samples

这篇关于在.NET 2中搜索XML文件以查找具有特定属性值的节点的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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