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("//book[contains(title, 'Harry')]");

nodes就是模糊查找结果

相关推荐
山檐雾2 小时前
OctreeNode
unity·c#·八叉树
QfC92C02p6 小时前
C# 中的 Span 和内存:.NET 中的高性能内存处理
java·c#·.net
Yuri X-20217 小时前
VS2022实战测试题——2
程序人生·c#·个人开发·visual studio
公子小六7 小时前
基于.NET的Windows窗体编程之WinForms布局简介
windows·microsoft·c#·.net
zaim17 小时前
计算机的错误计算(二百二十六)
java·python·c#·c·错数·mpmath
William_cl8 小时前
[特殊字符]C# ASP.NET 架构封神之路:分层 + 仓储 + EFCore,写出企业级可维护代码!
架构·c#·asp.net
tq6J5Yg148 小时前
.NET 10 & C# 14 New Features 新增功能介绍-带修饰符的简单 lambda 参数
开发语言·c#·.net
fe7tQnVan8 小时前
从玩具到生产:基于 ChromaDB 打造工程级 RAG 系统
开发语言·c#
ySq0REx018 小时前
.NET 10 & C# 14 New Features 新增功能介绍-.NET CLI工具改进
开发语言·c#·.net
张人玉8 小时前
C#程序设计编程二维码识别程序
开发语言·c#·二维码