C# 写入XML文档三种方法详细介绍
发布时间:2021-11-08 23:20:08
标签:C#写入XML文档
我在以前的博客中介绍了如何使用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是最简便的。
0
投稿
猜你喜欢
- Java 实现汉字转换为拼音转换类public class PINYINChinese { private static int
- properties和yml的区别这几天刚好看到Spring Boot当中有两种配置文件的方式,但是这两种配置方式有什么区别呢?proper
- 使用flatMap列出子目录前面已经看到如何列出指定目录下的文件了。我们再来看下如何遍历指定目录的直接子目录(深度为1),先实现一个简单的版
- 1.算法效率算法效率分析分为两种:第一种是时间效率,第二种是空间效率。时间效率被称为时间复杂度,而空间效率被称作空间复杂度。时间复杂度主要衡
- 使用Regex类需要引用命名空间:using System.Text.RegularExpressions;一、利用Regex类实现验证示例
- 关于页面渲染其实在工作中,一直都是前后端分离,也就是说,我的工作从来都是提供接口,而不写 html css js 之类的,所以在这方面也没有
- 如下所示:# ===============================================================
- 本文实例为大家分享了Struts2框架拦截 器实例的示例代码,供大家参考,具体内容如下在看拦截 器的小例子的前我们先来看看sturts2的原
- 1、什么是反射?在java开发中有一个非常重要的概念就是java反射机制,也是java的重要特征之一。反射的概念是由Smith在1982年首
- idea切换分支时,修改过的代码文件全部不见了找了一下问题,切换分支时,idea自动会创建暂存文件,点开,右边View --> 即可显
- 最近因为项目的国际化的需要,需要对整个项目的100来个插件做国际化,这是一件痛苦的事情,因为纯体力劳动。为了省点工作量,想着能不能写个程序批
- 关于 swagger 本文不再赘述,网上文章很多。本文要讲的是Knife4j3.0.3 整合SpringBoot 2.6.4,因为 knif
- dependencyManagement一般出现在最顶层父工程的pom文件中,它的作用是用来管理jar版本,让子项目引用一个依赖而不用指明版
- 场景做一个消息中心,专门负责发送消息。消息分为几种渠道,包括手机通知(Push)、短信(SMS)、邮件(Email),Websocket等渠
- 前言纸上得来终觉浅,绝知此事要躬行一.会话引入什么是会话?会话用来识别不同的客户端,客户端和服务器之间发生的一系列连续的请求和响应的过程,当
- 关于约瑟夫环的基本知识:罗马人攻占了乔塔帕特,41人藏在一个山洞中躲过了这场浩劫。这41个人中,包括历史学家josephus和他的一个朋友。
- 本文实例为大家分享了MapReduce实现决策树算法的具体代码,供大家参考,具体内容如下首先,基于C45决策树算法实现对应的Mapper算子
- 定义在一幅无向图G=(V,E) 中,(u,v) 为连接顶点u和顶点v的边,w(u,v)为边的权重,若存在边的子集T&am
- 1.pom.xml<?xml version="1.0" encoding="UTF-8"?&
- 前言:本质上来说,CoreCLR 也是 C++ 写的,所以也逃不过用 虚表 来实现多态的玩法, 不过玩法也稍微复杂了一些,希望本篇对大家有帮