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

相关推荐
T.Ree.1 天前
汇编_读写内存
开发语言·汇编·c#
czhc11400756631 天前
C#1114 枚举
开发语言·c#
曹牧1 天前
C#中,GetValueOrDefault方法
c#
SunnyDays10111 天前
使用 C# 实现 Excel 与 DataTable 相互转换
c#·excel转datatable·datatable转excel
獨枭2 天前
C# 本地项目引用失效与恢复全攻略
开发语言·c#·visual studio
清风与日月2 天前
c# 上位机作为控制端与下位机通信方式
单片机·嵌入式硬件·c#
烛阴2 天前
从零开始掌握C#核心:变量与数据类型
前端·c#
yue0082 天前
C# 生成指定位数的编号
开发语言·c#
红黑色的圣西罗2 天前
C# List.Sort方法总结
开发语言·c#