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

相关推荐
鱼听禅39 分钟前
C# 学习笔记-使用 类型化客户端搭建HTTP客户端服务
学习·http·c#·工厂方法模式
geovindu3 小时前
CSharp:Condition Variable Pattern
后端·设计模式·c#·.net·.netcore·条件变量模式·同步型模式
淡海水3 小时前
11-03-Unity-List-T-和Dictionary-TKey-TValue-的性能调优实战
算法·unity·c#·list·dictionary
fogota4 小时前
C# 如何调用系统图标
c#·.net
△曉風殘月〆4 小时前
.NET是什么?为什么要选择.NET?
c#·.net·.net core·clr
fogota4 小时前
C# WPF 按钮加载字体图标 / 动物头像
c#·.net·wpf
fogota4 小时前
C# WPF 按钮加载系统图标 / 动物头像
c#·.net·wpf
军训猫猫头5 小时前
C# 封装 MySQL 自动写入与查询工具类(基于 MySqlConnector)
mysql·c#·.net·wpf
zxy284722530115 小时前
C# 音频倍速播放
c#·音频·naudio·soundtouch·倍速
Water_Sunzhipeng18 小时前
HandyControl Demo 设置特定框架记录
c#