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

相关推荐
anlog7 小时前
ini文件读取,EXE同目录文件读取
c#·ini
rockey6277 小时前
AScript之编译递归函数
c#·.net·script·动态脚本
yue00813 小时前
winform控件篇-按钮
c#
DevOpenClub16 小时前
PDF 多格式解析如何避免混用输出:TEXT、HTML、XML 与 TAG 数据契约
xml·前端·数据库·pdf·html
DevOpenClub17 小时前
Markdown、HTML 和 PPT 如何稳定交付:文档转换任务的幂等发布流程
开发语言·前端·c#·html·powerpoint
软件黑马王子18 小时前
3.单例模式问题1:构造函数
开发语言·单例模式·c#
软件黑马王子19 小时前
4.单例模式问题2:重复挂载
开发语言·单例模式·c#
夏霞20 小时前
c# 不支持的目标框架 解决方案
开发语言·c#
软件黑马王子20 小时前
5.单例模式问题3:多线程问题
单例模式·前端框架·c#
geovindu20 小时前
CSharp: Template Method Pattern
开发语言·后端·c#·.net·模板方法模式·行为模式