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就是模糊查找结果

相关推荐
颜x小3 小时前
[C#] C++与c#语法对比
开发语言·c++·c#
weixin_423652133 小时前
C#使用JObject
开发语言·c#
Z5998178414 小时前
c#软件开发学习笔记--Modbus-RTU通讯
笔记·学习·c#
用户298698530147 小时前
在 PC 上将 HTML 转换为 PDF 的3种有效方法
人工智能·c#·html
Rotion_深10 小时前
C# 设计TCP Server
开发语言·tcp/ip·c#
csdn_aspnet11 小时前
C# 从凸包中删除点(Deleting points from Convex Hull)
开发语言·c#
czhc114007566312 小时前
7.28 从“图形状态切换“理解 Parse 与状态驱动模式
c#
逻极13 小时前
C# 从入门到精通:现代云原生与AI应用开发实战
ai·云原生·c#·.net
geovindu1 天前
CSharp: Breadth First Search Algorithm and Depth First Search Algorithm
开发语言·后端·算法·c#·.net·搜索算法
小羊先生car1 天前
RTOS-F429-HAL-(动/静态)任务的创建(2026/7/27)
开发语言·算法·c#