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

相关推荐
yangshuquan1 小时前
关于 C# 函数参数修饰符 out 和 in 的真相
c#·参数·in·修饰符·out
全栈师1 小时前
C#中控制权限的逻辑写法
开发语言·c#
夏霞2 小时前
c# 使用vs code 创建.net8.0以及.net6.0 webApi项目的教程
开发语言·c#·.net
故事不长丨3 小时前
C#线程的使用
java·microsoft·c#
小小8程序员3 小时前
C# XAML中x:Type的用法详解
开发语言·ui·c#
周杰伦fans4 小时前
在C#中,`StringContent` 是 `HttpContent` 的一个派生类
开发语言·数据库·c#
苦荞米5 小时前
异步方法-C#中坑最大最深的功能
开发语言·c#
时光追逐者6 小时前
C# 中 ?、??、??=、?: 、?. 、?[] 各种问号的用法和说明
开发语言·c#·.net·.net core
周杰伦fans7 小时前
C#中OrderByDescending 是LINQ扩展方法之一
开发语言·c#·linq
周杰伦fans7 小时前
C# 中 SQL Server 数据库调优指南(小白友好版)
开发语言·数据库·c#