C# 写入XML文档三种方法详细介绍

时间:2021-11-08 23:20:08 

我在以前的博客中介绍了如何使用XmlDocument类对XML进行操作,以及如何使用LINQ to XML对XML进行操作。它们分别使用了XmlDocument类和XDocument类。在本文中,我再介绍一个类,XmlTextWriter。我们分别用这三个类将同样的xml内容写入文档,看一看哪种写法最直观、简便。
我们要写入的XML文档内容为


<?xml version="1.0" encoding="UTF-8"?>
<Contacts>
<Contact id="01">
<Name>Daisy Abbey</Name>
<Gender>female</Gender>
</Contact>
</Contacts>


(1)使用XmlDocument类


var xmlDoc = new XmlDocument();
//Create the xml declaration first
xmlDoc.AppendChild(xmlDoc.CreateXmlDeclaration("1.0", "utf-8", null));
//Create the root node and append into doc
var el = xmlDoc.CreateElement("Contacts");
xmlDoc.AppendChild(el);
// Contact
XmlElement elementContact = xmlDoc.CreateElement("Contact");
XmlAttribute attrID = xmlDoc.CreateAttribute("id");
attrID.Value = "01";
elementContact.Attributes.Append(attrID);
el.AppendChild(elementContact);
// Contact Name
XmlElement elementName = xmlDoc.CreateElement("Name");
elementName.InnerText = "Daisy Abbey";
elementContact.AppendChild(elementName);
// Contact Gender
XmlElement elementGender = xmlDoc.CreateElement("Gender");
elementGender.InnerText = "female";
elementContact.AppendChild(elementGender);
xmlDoc.Save("test1.xml");


(2)使用LINQ to XML 的XDocument类


var doc = new XDocument(
new XElement("Contacts",
new XElement("Contact",
new XAttribute("id", "01"),
new XElement("Name", "Daisy Abbey"),
new XElement("Gender", "female")
)
)
);
doc.Save("test2.xml");


(3) 使用XmlTextWriter类


String filename = String.Concat("test3.xml");
using (StreamWriter sw = new StreamWriter(filename))
{
// Create Xml Writer.
XmlTextWriter xmlWriter = new XmlTextWriter(sw);
// 也可以使用public XmlTextWriter(string filename, Encoding encoding)来构造
// encoding默认为 UTF-8.
//XmlTextWriter writer = new XmlTextWriter("test3.xml", null);
// Set indenting so that its easier to read XML when open in Notepad and such apps.
xmlWriter.Formatting = Formatting.Indented;
// This will output the XML declaration
xmlWriter.WriteStartDocument();
xmlWriter.WriteStartElement("Contacts");
xmlWriter.WriteStartElement("Contact");
xmlWriter.WriteAttributeString("id", "01");
xmlWriter.WriteElementString("Name", "Daisy Abbey");
xmlWriter.WriteElementString("Gender", "female");
// close contact </contact>
xmlWriter.WriteEndElement();
// close contacts </contact>
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
xmlWriter.Close();
}


从上面的代码基本上还是可以看出来,使用LINQ to XML是最简便的。

标签:C#写入XML文档
0
投稿

猜你喜欢

  • java 装饰模式(Decorator Pattern)详解及实例代码

    2023-09-07 03:13:08
  • C#中WPF内存回收与释放LierdaCracker的实现

    2022-11-13 00:15:44
  • Zookeeper事务日志预分配空间解读

    2022-03-16 22:36:49
  • 5分钟快速学会spring boot整合JdbcTemplate的方法

    2022-04-01 15:56:11
  • Java JVM内存区域详解

    2021-05-25 18:44:43
  • 深入了解Java ServletContext

    2023-11-08 22:36:27
  • Java线程池高频面试题总结

    2023-10-22 02:12:34
  • c#获取本机在局域网ip地址的二种方法

    2023-01-20 00:19:47
  • Java查看线程运行状态的方法详解

    2023-05-23 12:21:10
  • Spring Boot加密配置文件方法介绍

    2023-09-11 17:15:58
  • Java reservedcodecachesize虚拟机参数案例详解

    2022-03-15 12:18:48
  • es(elasticsearch)整合SpringCloud(SpringBoot)搭建教程详解

    2023-12-06 07:34:14
  • Java分布式服务框架Dubbo介绍

    2022-09-16 01:27:53
  • 详解Servlet3.0新特性(从注解配置到websocket编程)

    2023-08-08 14:29:48
  • Java之 TreeSet的详细使用说明

    2021-08-22 14:52:22
  • java list随机抽取元素的案例

    2023-12-22 22:21:59
  • Java数据结构之链表详解

    2023-10-20 09:14:08
  • Centos中安装jdk案例讲解

    2023-04-30 00:37:50
  • 深入C# 内存管理以及优化的方法详解

    2021-10-24 14:24:03
  • java根据模板导出PDF的详细实现过程

    2022-04-07 21:48:52
  • asp之家 软件编程 m.aspxhome.com