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

相关推荐
1-1=05 分钟前
.NET Core 输出日志
c#·.net·.netcore
曹牧15 分钟前
C#里引用ActiveX控件
开发语言·c#
宇卿.40 分钟前
C#委托详解
开发语言·c#
努力,努力再努力1 小时前
零依赖 C# Office 库到底怎么用——四个场景讲透 Wisdom.Office
c#
2501_930707785 小时前
使用C#代码在 PDF 文档中创建交互操作
pdf·c#·交互
zimoyin14 小时前
在 C# 里嵌入 JavaScript:用 Jint 做可热更新的自动化脚本(从入门到踩坑)
javascript·c#·自动化
小羊没烦恼!16 小时前
异步编程:线程概述及使用
java·开发语言·mysql·算法·c#
c#上位机18 小时前
C#上位机项目实战——exe项目调试更改dll中源代码
开发语言·c#
淡海水19 小时前
06-02-源码-增量标记算法实现
算法·unity·c#·gc
淡海水20 小时前
06-01-源码-Incremental GC总览与设计
unity·c#·gc