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 小时前
JSON的缩进格式方式和紧凑格式方式
c#·json
Eiceblue16 小时前
使用 C# 发送电子邮件(支持普通文本、HTML 和附件)
开发语言·c#·html·visual studio
小小小小王王王16 小时前
hello判断
开发语言·c#
金增辉18 小时前
基于C#的OPCServer应用开发,引用WtOPCSvr.dll
c#
future141220 小时前
C#学习日记
开发语言·学习·c#
傻啦嘿哟21 小时前
Python 办公实战:用 python-docx 自动生成 Word 文档
开发语言·c#
唐青枫1 天前
C#.NET log4net 详解
c#·.net
Nemo_XP1 天前
HttpHelper类处理两种HTTP POST请求
c#
lijingguang2 天前
在C#中根据URL下载文件并保存到本地,可以使用以下方法(推荐使用现代异步方式)
开发语言·c#
¥-oriented2 天前
【C#中路径相关的概念】
开发语言·c#