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

相关推荐
kaikaile19957 小时前
数字全息图处理系统(C# 实现)
开发语言·c#
wearegogog12312 小时前
C# .NET 文件比较工具 WinForms
开发语言·c#·.net
糖不吃12 小时前
WPF值转换器
c#
摇滚侠12 小时前
Spring 零基础入门到进阶 基于 XML 管理 Bean 14-28
xml·数据库·spring
Popeye-lxw14 小时前
由罗技 K380 键盘 FN 键模式切换引发的血案
c#
FL162386312914 小时前
C# OpenCvSharp 基于霍夫变换直线检测的文本图像倾斜校正文本图像倾斜校
开发语言·c#
aini_lovee15 小时前
C# 快递单打印系统(万能套打系统)
开发语言·c#
白菜上路15 小时前
C# Serilog.AspNetCore基本使用
c#·serilog
小白不白11116 小时前
C# WinForm 与 VP 二次开发
开发语言·c#
SunnyDays101117 小时前
如何使用 C# 自动调整 Excel 行高和列宽
开发语言·c#·excel