XML selectNodes 模糊查找

cs 复制代码
    public static XmlElement[] FuzzyFindNode(string xmlPath, string key, string value=null)
    {
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(xmlPath);  
        string xpath = $"//节点名字[contains(@{key},'{value}')]"; 
        XmlNodeList nodes = xmlDoc.SelectNodes(xpath); 
        XmlElement[] eles = nodes.Cast<XmlElement>().ToArray();
        return eles;


    }

示例:

XML 复制代码
<bookstore>
	<book category="cooking">
		<title lang="en">Everyday Italian</title>
		<author>Giada De Laurentiis</author>
		<year>2005</year>
		<price>30.00</price>
	</book>
	<book category="children">
		<title lang="en">Harry Potter</title>
		<author>J.K. Rowling</author>
		<year>2005</year>
		<price>29.99</price>
	</book>
	<book category="web">
		<title lang="en">XQuery Kick Start</title>
		<author>James McGovern</author>
		<year>2003</year>
		<price>49.99</price>
	</book>
	<book category="web">
		<title lang="en">Learning XML</title>
		<author>Erik T. Ray</author>
		<year>2003</year>
		<price>39.95</price>
	</book>
</bookstore>

doc.Load("books.xml");

XmlNodeList nodes = doc.SelectNodes("//bookcontains(title, 'Harry')");

nodes就是模糊查找结果

相关推荐
看浪的路人10 小时前
第10讲:运维 Agent——从「被动救火」到「自动驾驶」
c#·linq
Jazz_z13 小时前
C# 实现 Word 文档内容复制
开发语言·c#·word
huaqianzkh2 天前
WMI 查询入门到实战:从概念、WQL 语法到 C# 采集系统与硬件信息完整示例
开发语言·c#
2395730492 天前
Avalonia 12 代码编辑器性能横评:TKCode.Arc CodeEditor vs AvaloniaEdit 12.0(含深度调优实战)
c#·.net·avalonia·codeeditor·tkcode.arc
张人玉4 天前
C#如何使用FreeSql连接数据库
数据库·c#·freesql
步峰代理-盾山4 天前
R语言爬虫实战:使用httr与XML解析网页数据并进行统计分析
xml·开发语言·爬虫·r语言·网络爬虫
吴可可1234 天前
C#打断多段线实现方法
c#
Kari114 天前
医药和制造行业的外文文档批量翻译,排版怎么保住?
开发语言·c#·制造
Warson_L5 天前
.NET 8 速成笔记 1:用 VS Code 做 C# / .NET 开发
c#·.net
Warson_L5 天前
.NET 8 速成笔记 9:C# 项目常见场景(基础设施篇)
c#·.net