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

相关推荐
xiaoshuaishuai81 小时前
C# 内存管理与资源泄漏
开发语言·c#
Meteors.1 小时前
安卓源码阅读——01.grade设置binding为true时,xml如何进行映射
android·xml
z落落5 小时前
C#参数区别
java·算法·c#
影寂ldy5 小时前
C#随机数
开发语言·c#
雪豹阿伟5 小时前
11.C# —— 数组属性、高级方法与 Lambda 表达式
c#·上位机
唐青枫6 小时前
C#.NET YARP 服务发现实战:接入 Consul 和 Kubernetes 动态发现后端服务
c#·.net
largecode7 小时前
座机号码认证如何操作?申请热线实名名片,树立统一官方客服形象
linux·sql·华为·c#·.net·wpf·harmonyos
小满Autumn9 小时前
WPF 入门:XAML 语法、布局与数据绑定
microsoft·c#·.net·wpf
程序员buddha9 小时前
传统 Spring 框架,XML 配置 Bean 的方式
xml·java·spring
曹牧10 小时前
LINQ:Select
c#·linq