XmlDocument类的WriteContentTo和WriteTo方法有什么区别吗?起初我也被忽悠了,我们一起来验证下。(本文中有一个完整生成XML文件的C# Code范例)
今天在看《C#高级编程(第四版)》第21章 处理XML 之 使用XmlDocument对象
书中有段原文,引起了我的注意: “在这个示例中,创建一个新XmlTextWriter,把它传送给WriteContentTo方法。WriteContentTo和WriteTo方法都带一个XmlTextWriter参数。WriteContentTo把当前节点 及其所有的子节点都保存到XmlTextWriter,而WriteTo只保存当前节点。”
我很遗憾,我还是没搞懂两个方法之间到底有什么区别,于是决定写一个例子来帮助我理解:

将XmlDocument对象生成Xml文件 class Program
{
static void Main(string[] args)
{
XmlDocument doc = new XmlDocument();
XmlDeclaration xmlDeclaration = doc.CreateXmlDeclaration("1.0", "utf-8", null);
doc.InsertBefore(xmlDeclaration, doc.DocumentElement);
XmlElement bookStore = doc.CreateElement("bookstore");
XmlElement book = doc.CreateElement("book");
book.SetAttribute("genre", "autobiography");
book.SetAttribute("publicationdate", "1991");
book.SetAttribute("ISBN", "1-861003-11-0");
XmlElement title = doc.CreateElement("title");
title.InnerText = "The Autobiography of Benjamin Franklin";
XmlElement author = doc.CreateElement("author");
XmlElement firstName = doc.CreateElement("first-name");
firstName.InnerText = "Benjamin";
XmlElement lastName = doc.CreateElement("last-name");
lastName.InnerText = "Franklin";
author.AppendChild(firstName);
author.AppendChild(lastName);
XmlElement price = doc.CreateElement("price");
price.InnerText = "11.
99"; book.AppendChild(title); book.AppendChild(author); book.AppendChild(price); bookStore.AppendChild(book); doc.AppendChild(bookStore); using (XmlTextWriter writer = new XmlTextWriter("book.xml", null)) { writer.Formatting = Formatting.Indented; doc.WriteContentTo(writer); //doc.WriteTo(writer); writer.Close(); } } }
运行成功,生成的Xml文件内容如下:

book.xml<?xml version="1.0" encoding="utf-8"?>
<bookstore>
<book genre="autobiography" publicationdate="1991" ISBN="1-861003-11-0">
<title>The Autobiography of Benjamin Franklin</title>
<author>
<first-name>Benjamin</first-name>
<last-name>Franklin</last-name>
</author>
<price>11.99</price>
</book>
</bookstore>
当我将代码中的doc.WriteContentTo(writer)换成doc.WriteTo(writer),F5运行生成的book.xml文件内容仍然一模一样,我还是没搞明白两个方法之间到底有什么区别。接着我又开始使劲的折腾我的代码,得到的结果是WriteContentTo和WriteTo方法最终生成的Xml文件还是一样的。
难道他们的功能是一样的,为什么要弄两个名称呢?
没辙,突然想到用Reflector反编译看看吧:

反编译后的WriteContentTo和WriteTo方法源码public override void WriteContentTo(XmlWriter xw)
{
foreach (XmlNode node in this)
{
node.WriteTo(xw);
}
}
public override void WriteTo(XmlWriter w)
{
this.WriteContentTo(w);
}
真相终于大白,XmlDocument类的WriteContentTo和WriteTo方法都是重载基类的(这点我一开始就知道),WriteTo方法直接调用WriteContentTo方法。它们的功能是一模一样的,那为什么不说明一下呢,唉...
最后我们在VS里面F12追踪一下,看看他们的定义:

WriteContentTo和WriteTo方法的定义描述 //
// 摘要:
// 将 XmlDocument 节点的所有子级保存到指定的 System.Xml.XmlWriter 中。
//
// 参数:
// xw:
// 要保存到其中的 XmlWriter。
public override void WriteContentTo(XmlWriter xw);
//
// 摘要:
// 将 XmlDocument 节点保存到指定的 System.Xml.XmlWriter。
//
// 参数:
// w:
// 要保存到其中的 XmlWriter。
public override void WriteTo(XmlWriter w);
你是不是也很迷糊啊?
【推荐】国内首个AI IDE,深度理解中文开发场景,立即下载体验Trae
【推荐】编程新体验,更懂你的AI,立即体验豆包MarsCode编程助手
【推荐】抖音旗下AI助手豆包,你的智能百科全书,全免费不限次数
【推荐】轻量又高性能的 SSH 工具 IShell:AI 加持,快人一步
· 如何编写易于单元测试的代码
· 10年+ .NET Coder 心语,封装的思维:从隐藏、稳定开始理解其本质意义
· .NET Core 中如何实现缓存的预热?
· 从 HTTP 原因短语缺失研究 HTTP/2 和 HTTP/3 的设计差异
· AI与.NET技术实操系列:向量存储与相似性搜索在 .NET 中的实现
· 周边上新:园子的第一款马克杯温暖上架
· Open-Sora 2.0 重磅开源!
· 分享 3 个 .NET 开源的文件压缩处理库,助力快速实现文件压缩解压功能!
· Ollama——大语言模型本地部署的极速利器
· [AI/GPT/综述] AI Agent的设计模式综述