跳到主要内容

C# XML 处理

介绍

XML(可扩展标记语言)是一种用于存储和传输数据的标记语言。它被广泛用于配置文件、数据交换和 Web 服务中。在 C# 中,处理 XML 文件是一项常见的任务,尤其是在需要读取或写入结构化数据时。

C# 提供了多种处理 XML 的方式,包括使用 System.Xml 命名空间中的类,如 XmlDocumentXmlReaderXmlWriter,以及使用 LINQ to XML 进行更现代的 XML 处理。

本文将逐步介绍如何在 C# 中处理 XML 文件,包括读取、写入和修改 XML 数据。

读取 XML 文件

使用 XmlDocument 读取 XML

XmlDocument 类提供了一种将整个 XML 文档加载到内存中的方式,适合处理较小的 XML 文件。

csharp
using System;
using System.Xml;

class Program
{
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("example.xml");

XmlNode root = xmlDoc.DocumentElement;
Console.WriteLine("Root element: " + root.Name);

foreach (XmlNode node in root.ChildNodes)
{
Console.WriteLine("Node: " + node.Name + ", Value: " + node.InnerText);
}
}
}

输入 (example.xml):

xml
<bookstore>
<book>
<title>Learning C#</title>
<author>John Doe</author>
</book>
<book>
<title>Advanced C#</title>
<author>Jane Smith</author>
</book>
</bookstore>

输出:

Root element: bookstore
Node: book, Value: Learning C#John Doe
Node: book, Value: Advanced C#Jane Smith

使用 XmlReader 读取 XML

XmlReader 类提供了一种流式读取 XML 的方式,适合处理较大的 XML 文件。

csharp
using System;
using System.Xml;

class Program
{
static void Main()
{
using (XmlReader reader = XmlReader.Create("example.xml"))
{
while (reader.Read())
{
if (reader.NodeType == XmlNodeType.Element)
{
Console.WriteLine("Element: " + reader.Name);
}
else if (reader.NodeType == XmlNodeType.Text)
{
Console.WriteLine("Text: " + reader.Value);
}
}
}
}
}

输出:

Element: bookstore
Element: book
Element: title
Text: Learning C#
Element: author
Text: John Doe
Element: book
Element: title
Text: Advanced C#
Element: author
Text: Jane Smith

写入 XML 文件

使用 XmlDocument 写入 XML

XmlDocument 类不仅可以用于读取 XML,还可以用于创建和修改 XML 文档。

csharp
using System;
using System.Xml;

class Program
{
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
XmlElement root = xmlDoc.CreateElement("bookstore");
xmlDoc.AppendChild(root);

XmlElement book = xmlDoc.CreateElement("book");
root.AppendChild(book);

XmlElement title = xmlDoc.CreateElement("title");
title.InnerText = "Learning C#";
book.AppendChild(title);

XmlElement author = xmlDoc.CreateElement("author");
author.InnerText = "John Doe";
book.AppendChild(author);

xmlDoc.Save("output.xml");
}
}

输出 (output.xml):

xml
<bookstore>
<book>
<title>Learning C#</title>
<author>John Doe</author>
</book>
</bookstore>

使用 XmlWriter 写入 XML

XmlWriter 类提供了一种流式写入 XML 的方式,适合生成较大的 XML 文件。

csharp
using System;
using System.Xml;

class Program
{
static void Main()
{
using (XmlWriter writer = XmlWriter.Create("output.xml"))
{
writer.WriteStartDocument();
writer.WriteStartElement("bookstore");

writer.WriteStartElement("book");
writer.WriteElementString("title", "Learning C#");
writer.WriteElementString("author", "John Doe");
writer.WriteEndElement();

writer.WriteEndElement();
writer.WriteEndDocument();
}
}
}

输出 (output.xml):

xml
<bookstore>
<book>
<title>Learning C#</title>
<author>John Doe</author>
</book>
</bookstore>

修改 XML 文件

使用 XmlDocument 修改 XML

XmlDocument 类允许你修改现有的 XML 文档。

csharp
using System;
using System.Xml;

class Program
{
static void Main()
{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load("example.xml");

XmlNode root = xmlDoc.DocumentElement;
XmlNode firstBook = root.SelectSingleNode("book");

if (firstBook != null)
{
XmlNode titleNode = firstBook.SelectSingleNode("title");
if (titleNode != null)
{
titleNode.InnerText = "Mastering C#";
}
}

xmlDoc.Save("modified.xml");
}
}

输入 (example.xml):

xml
<bookstore>
<book>
<title>Learning C#</title>
<author>John Doe</author>
</book>
<book>
<title>Advanced C#</title>
<author>Jane Smith</author>
</book>
</bookstore>

输出 (modified.xml):

xml
<bookstore>
<book>
<title>Mastering C#</title>
<author>John Doe</author>
</book>
<book>
<title>Advanced C#</title>
<author>Jane Smith</author>
</book>
</bookstore>

实际应用场景

配置文件管理

XML 常用于存储应用程序的配置信息。例如,一个应用程序的配置文件可能如下所示:

xml
<configuration>
<appSettings>
<add key="Server" value="localhost" />
<add key="Port" value="8080" />
</appSettings>
</configuration>

使用 XmlDocumentXmlReader,你可以轻松读取和修改这些配置。

数据交换

XML 也常用于不同系统之间的数据交换。例如,一个 Web 服务可能返回 XML 格式的数据,你可以使用 C# 解析这些数据并提取所需的信息。

总结

在 C# 中处理 XML 文件是一项重要的技能,尤其是在处理配置文件、数据交换和 Web 服务时。本文介绍了如何使用 XmlDocumentXmlReaderXmlWriter 来读取、写入和修改 XML 文件。我们还探讨了一些实际应用场景,如配置文件管理和数据交换。

附加资源

练习

  1. 创建一个 XML 文件,包含多个书籍信息,并使用 XmlDocument 读取并打印每本书的标题和作者。
  2. 使用 XmlWriter 创建一个新的 XML 文件,包含多个学生的信息(如姓名、年龄、成绩)。
  3. 修改现有的 XML 文件,将某个节点的值更改为新的值,并保存修改后的文件。

通过完成这些练习,你将更好地掌握 C# 中的 XML 处理技术。