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

相关推荐
好大的月亮14 分钟前
mybatis在xml中使用OGNL取值简述
xml·mybatis
jiushidt1 小时前
Things About ArcGISPro
arcgis·c#·.net·arcgispro
雪雁2 小时前
CodeSpirit 多语言国际化使用指南(Beta)
c#·asp.net·.net 10·codespirit
自己的九又四分之三站台2 小时前
写一个简单的DebugView
c#
CreasyChan2 小时前
C#中单个下划线的语法与用途详解
前端·c#
m5655bj2 小时前
如何通过 C# 实现 PDF 页面裁剪
前端·pdf·c#
STARBLOCKSHADOW2 小时前
【C#】VS中打包C#桌面软件为exe文件
c#·vs·打包·桌面程序
步步为营DotNet2 小时前
深度解析C# 11 的Required成员:编译期验证逻辑与稳健编程实践
java·服务器·c#
草莓熊Lotso2 小时前
Qt 入门核心指南:从框架认知到环境搭建 + Qt Creator 实战
xml·开发语言·网络·c++·人工智能·qt·页面
小夏卷编程3 小时前
Mybaits 使用过程中mapper.xml foreach List<String[]>的时候报错
xml