XML 通过XmlDocument与XDocument方式写入XML
在C#开发中,操作XML文件是常见的需求,主流的实现方式有两种:基于传统DOM模型的XmlDocument类和基于LINQ to XML的XDocument类。两种方式各有适用场景,下面分别介绍它们的使用方法,并给出完整的写入XML的代码示例。
一、使用XmlDocument写入XML
XmlDocument是.NET Framework早期提供的XML操作类,遵循W3C的DOM标准,操作方式更贴近XML的节点结构,适合熟悉传统DOM模型的开发者使用。它的核心思路是先构建XML的声明、根节点,再逐步添加子节点和属性,最后保存到文件。
下面的示例会创建一个包含学生信息的XML文件,结构包含学生ID、姓名、年龄三个字段:
using System;
using System.Xml;
namespace XmlDocumentDemo
{
class Program
{
static void Main(string[] args)
{
// 创建XmlDocument实例
XmlDocument xmlDoc = new XmlDocument();
// 创建XML声明,版本1.0,编码UTF-8
XmlDeclaration xmlDecl = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
xmlDoc.AppendChild(xmlDecl);
// 创建根节点Students
XmlElement root = xmlDoc.CreateElement("Students");
xmlDoc.AppendChild(root);
// 创建第一个学生节点
XmlElement student1 = xmlDoc.CreateElement("Student");
// 给学生节点添加ID属性
student1.SetAttribute("ID", "1001");
// 创建姓名字子节点
XmlElement name1 = xmlDoc.CreateElement("Name");
name1.InnerText = "张三";
student1.AppendChild(name1);
// 创建年龄子节点
XmlElement age1 = xmlDoc.CreateElement("Age");
age1.InnerText = "20";
student1.AppendChild(age1);
// 将第一个学生节点添加到根节点
root.AppendChild(student1);
// 创建第二个学生节点
XmlElement student2 = xmlDoc.CreateElement("Student");
student2.SetAttribute("ID", "1002");
XmlElement name2 = xmlDoc.CreateElement("Name");
name2.InnerText = "李四";
student2.AppendChild(name2);
XmlElement age2 = xmlDoc.CreateElement("Age");
age2.InnerText = "21";
student2.AppendChild(age2);
root.AppendChild(student2);
// 保存XML到文件,路径可根据实际需求调整
string filePath = "Students_XmlDocument.xml";
xmlDoc.Save(filePath);
Console.WriteLine($"XmlDocument方式写入XML完成,文件路径:{filePath}");
}
}
}运行上述代码后,生成的XML文件内容如下:
<?xml version="1.0" encoding="UTF-8"?>
<Students>
<Student ID="1001">
<Name>张三</Name>
<Age>20</Age>
</Student>
<Student ID="1002">
<Name>李四</Name>
<Age>21</Age>
</Student>
</Students>二、使用XDocument写入XML
XDocument是.NET 3.5之后引入的LINQ to XML的核心类,语法更简洁,支持函数式构造,代码可读性更高,是目前更推荐的XML操作方式。它可以直接通过链式调用的方式构建整个XML结构,不需要逐个创建节点再追加。
下面的示例实现和上面XmlDocument完全相同的XML内容,对比可以看出语法的差异:
using System;
using System.Xml.Linq;
namespace XDocumentDemo
{
class Program
{
static void Main(string[] args)
{
// 直接通过函数式构造创建XDocument实例
XDocument xDoc = new XDocument(
// 创建XML声明
new XDeclaration("1.0", "UTF-8", null),
// 创建根节点Students
new XElement("Students",
// 第一个Student节点,包含属性ID和子节点
new XElement("Student",
new XAttribute("ID", "1001"),
new XElement("Name", "张三"),
new XElement("Age", "20")
),
// 第二个Student节点
new XElement("Student",
new XAttribute("ID", "1002"),
new XElement("Name", "李四"),
new XElement("Age", "21")
)
)
);
// 保存XML到文件,路径可根据实际需求调整
string filePath = "Students_XDocument.xml";
xDoc.Save(filePath);
Console.WriteLine($"XDocument方式写入XML完成,文件路径:{filePath}");
}
}
}运行后生成的XML文件和XmlDocument方式的结果完全一致,只是代码量更少,逻辑更清晰。如果需要后续查询、修改XML内容,XDocument配合LINQ查询也会更加方便。
三、两种方式的选择建议
- 如果是维护旧项目,原本使用了XmlDocument,或者开发者更熟悉DOM模型的操作方式,可以继续使用XmlDocument。
- 如果是新项目开发,推荐优先使用XDocument,它的语法更简洁,扩展性和可读性都更好,而且和LINQ结合使用可以大幅简化XML的查询和修改操作。
- 如果处理的XML文件非常大,两种方式都建议采用流式读取的方式避免内存占用过高,小文件场景下两种方式的性能差异可以忽略。
XmlDocumentXDocumentLINQ_to_XMLC#操作XMLXML写入 本作品最后修改时间:2026-05-22 23:50:57