XML操作中XDocument与XmlDocument搜索指定节点的方法对比
在.NET平台处理XML数据时,XDocument和XmlDocument是两类常用的API,前者属于LINQ to XML体系,后者属于传统DOM模型。两者都支持搜索指定节点,但在语法风格、查询能力和使用场景上存在明显差异,本文将通过具体示例说明两者的节点搜索方式。
一、XmlDocument的节点搜索方式
XmlDocument是.NET早期提供的DOM模型实现,它将整个XML文档加载到内存中,以树形结构存储节点。搜索节点时主要依赖SelectSingleNode和SelectNodes方法,配合XPath表达式定位目标节点。
下面以搜索包含指定属性值的<book>节点为例,演示XmlDocument的使用方式:
using System;
using System.Xml;
class XmlDocumentSearchDemo
{
static void Main()
{
// 加载XML文档
XmlDocument xmlDoc = new XmlDocument();
string xmlContent = @"
<bookstore>
<book category="programming">
<title>C#基础教程</title>
<author>张三</author>
<price>59.9</price>
</book>
<book category="literature">
<title>散文精选</title>
<author>李四</author>
<price>39.9</price>
</book>
<book category="programming">
<title>ASP.NET实战</title>
<author>王五</author>
<price>69.9</price>
</book>
</bookstore>";
xmlDoc.LoadXml(xmlContent);
// 搜索所有category为programming的book节点
XmlNodeList programmingBooks = xmlDoc.SelectNodes("//book[@category='programming']");
Console.WriteLine($"找到{programmingBooks.Count}本编程类书籍:");
foreach (XmlNode bookNode in programmingBooks)
{
string title = bookNode.SelectSingleNode("title").InnerText;
string author = bookNode.SelectSingleNode("author").InnerText;
Console.WriteLine($"书名:{title},作者:{author}");
}
// 搜索author为李四的book节点
XmlNode targetBook = xmlDoc.SelectSingleNode("//book[author='李四']");
if (targetBook != null)
{
string title = targetBook.SelectSingleNode("title").InnerText;
string price = targetBook.SelectSingleNode("price").InnerText;
Console.WriteLine($"\n找到李四的书籍:{title},价格:{price}");
}
}
}上述代码中,SelectNodes方法接收XPath表达式返回所有匹配的节点集合,SelectSingleNode则返回第一个匹配的节点。XPath语法的优势是表达简洁,适合熟悉XPath的开发者快速编写查询条件,但缺点是缺乏编译时检查,查询表达式错误时只能在运行时发现问题。
二、XDocument的节点搜索方式
XDocument是LINQ to XML的核心类,提供了更现代的函数式编程风格,支持直接使用LINQ查询语法搜索节点,同时也兼容XPath查询。相比XmlDocument,它的API更简洁,查询逻辑更直观,还支持延迟加载等特性。
同样以实现搜索指定节点为例,下面是XDocument的实现代码:
using System;
using System.Linq;
using System.Xml.Linq;
class XDocumentSearchDemo
{
static void Main()
{
// 加载XML文档
string xmlContent = @"
<bookstore>
<book category="programming">
<title>C#基础教程</title>
<author>张三</author>
<price>59.9</price>
</book>
<book category="literature">
<title>散文精选</title>
<author>李四</author>
<price>39.9</price>
</book>
<book category="programming">
<title>ASP.NET实战</title>
<author>王五</author>
<price>69.9</price>
</book>
</bookstore>";
XDocument xDoc = XDocument.Parse(xmlContent);
// 使用LINQ语法搜索所有category为programming的book节点
var programmingBooks = xDoc.Descendants("book")
.Where(book => book.Attribute("category")?.Value == "programming");
Console.WriteLine($"找到{programmingBooks.Count()}本编程类书籍:");
foreach (var bookElement in programmingBooks)
{
string title = bookElement.Element("title")?.Value;
string author = bookElement.Element("author")?.Value;
Console.WriteLine($"书名:{title},作者:{author}");
}
// 使用LINQ语法搜索author为李四的book节点
var targetBook = xDoc.Descendants("book")
.FirstOrDefault(book => book.Element("author")?.Value == "李四");
if (targetBook != null)
{
string title = targetBook.Element("title")?.Value;
string price = targetBook.Element("price")?.Value;
Console.WriteLine($"\n找到李四的书籍:{title},价格:{price}");
}
// 也可以使用XPath查询,需要引入System.Xml.XPath命名空间
// var xpathBooks = xDoc.XPathSelectElements("//book[@category='programming']");
}
}从上述代码可以看到,XDocument使用Descendants方法获取所有指定名称的后代节点,再配合LINQ的Where、FirstOrDefault等方法筛选符合条件的节点。这种方式的好处是查询逻辑完全由C#语法实现,有编译时检查,不容易出现语法错误,同时LINQ的链式调用也让逻辑更清晰。如果需要使用XPath,也可以通过扩展方法XPathSelectElements实现,兼顾习惯XPath的开发者需求。
三、两者的核心差异与选择建议
| 对比维度 | XmlDocument | XDocument |
|---|---|---|
| 所属体系 | 传统DOM模型 | LINQ to XML体系 |
| 查询方式 | 主要依赖XPath表达式 | 支持LINQ查询,也兼容XPath |
| 语法风格 | 方法调用配合字符串表达式 | 链式调用,函数式风格 |
| 编译时检查 | 无,XPath错误运行时暴露 | 有,LINQ语法错误编译时发现 |
| 适用场景 | 维护旧项目、熟悉XPath的场景 | 新项目开发、需要灵活查询的场景 |
如果是新开发的.NET项目,尤其是使用.NET Core及以上版本时,优先推荐选择XDocument,它的语法更友好,查询能力更强,也更符合现代C#的开发习惯。如果是维护已有使用XmlDocument的老项目,或者团队更熟悉XPath语法,可以继续使用XmlDocument完成节点搜索功能。
XDocumentXmlDocumentLINQ_to_XMLXPathXML节点搜索 本作品最后修改时间:2026-05-22 23:46:58