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

相关推荐
LongtengGensSupreme3 小时前
C# 中的 App.manifest 文件:优化应用程序配置与权限管理
数据库·ui·c#
capricorn12453 小时前
Unity热更方案HybridCLR+YooAsset,纯c#开发热更,保姆级教程,从零开始
unity·c#·游戏引擎
Android小贾5 小时前
HarmonyOS Next开发学习手册——单选框 (Radio)
xml·深度学习·学习·harmonyos·openharmony·组件化
Fountyuan6 小时前
C#正则表达式的基本语法
开发语言·正则表达式·c#
不爱敲代码的阿玲9 小时前
C# —— 方法参数ref和out的作用
开发语言·c#
栗克14 小时前
C#udpClient组播
前端·windows·udp·c#·.net·用户界面
白白白鲤鱼17 小时前
C# 入门—实现 Hello, World!
开发语言·c#
不爱敲代码的阿玲18 小时前
C# —— 子类访问父类
开发语言·c#
程序员阿明18 小时前
.net core接入nacos注册服务并使用配置中心
微服务·c#·.netcore
mingupup19 小时前
WPF/C#:BusinessLayerValidation
c#·wpf