基于C#中XmlWriter写入Xml的深入分析
时间:2021-06-29 10:00:54
假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml
1.如何使用XmlWriter写Xml文档声明
// WriteStartDocument方法可以接受一个bool参数(表示standalone,是否为独立文档)或者不指定参数standalone保持默认值
xmlWriter.WriteStartDocument(false|true);
注意在使用WriteStartDocument方法后最好调用xmlWrite.WriteEndDocument()方法来关闭所有可能未关闭标签
2.如何使用XmlWriter写xml节点以及属性
//写节点
xmlWriter.WriteStartElement("cat");
//给节点添加属性
xmlWriter.WriteAttributeString("color", "white");
//给节点内部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
或者通过WriteElementString(string,string)方法写xml节点同时写下节点值,如下
//通过WriteElementString可以添加一个节点同时添加节点内容
xmlWriter.WriteElementString("pig", "pig is great");
3.如何写CData
xmlWriter.WriteStartElement("dog");
//写CData
xmlWriter.WriteCData("<strong>dog is dog</strong>");
xmlWriter.WriteEndElement();
4.如何使用XmlWriter添加注释
xmlWriter.WriteComment("this is an example writed by https://www.jb51.net ");
5.如何设置XmlWriter的输出格式,解决输出UTF-16问题
设置xml输出格式,需要通过XmlWriterSettings类,如下代码
XmlWriterSettings settings = new XmlWriterSettings();
//要求缩进
settings.Indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加4个字节的非xml内容
settings.Encoding = new UTF8Encoding(false);
//设置换行符
settings.NewLineChars = Environment.NewLine;
完整的代码示例如下:
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;
using System.Xml;
namespace UseXmlWriter
{
class Program
{
static void Main(string[] args)
{
using (MemoryStream ms = new MemoryStream())
{
XmlWriterSettings settings = new XmlWriterSettings();
//要求缩进
settings.Indent = true;
//注意如果不设置encoding默认将输出utf-16
//注意这儿不能直接用Encoding.UTF8如果用Encoding.UTF8将在输出文本的最前面添加4个字节的非xml内容
settings.Encoding = new UTF8Encoding(false);
//设置换行符
settings.NewLineChars = Environment.NewLine;
using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
{
//写xml文件开始<?xml version="1.0" encoding="utf-8" ?>
xmlWriter.WriteStartDocument(false);
//写根节点
xmlWriter.WriteStartElement("root");
//写字节点
xmlWriter.WriteStartElement("cat");
//给节点添加属性
xmlWriter.WriteAttributeString("color", "white");
//给节点内部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
//通过WriteElementString可以添加一个节点同时添加节点内容
xmlWriter.WriteElementString("pig", "pig is great");
xmlWriter.WriteStartElement("dog");
//写CData
xmlWriter.WriteCData("<strong>dog is dog</strong>");
xmlWriter.WriteEndElement();
xmlWriter.WriteComment("this is an example writed by https://www.jb51.net ");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
//将xml内容输出到控制台中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
}
}
标签:c#,XmlWriter,Xml
![](/images/zang.png)
![](/images/jiucuo.png)
猜你喜欢
解决IDEA maven 项目修改代码不生效,mvn clean、install后才生效
2022-06-19 12:17:06
![](https://img.aspxhome.com/file/2023/2/97812_0s.jpg)
opencv实现轮廓高斯滤波平滑
2023-07-23 02:05:56
![](https://img.aspxhome.com/file/2023/4/82534_0s.jpg)
聊聊关于Java方法重写的反思
2022-10-26 00:36:12
SpringBoot整合Redis之编写RedisConfig
2023-08-29 02:35:57
java数据结构与算法之冒泡排序详解
2022-08-01 11:20:08
Java的MyBatis框架中实现多表连接查询和查询结果分页
2022-10-29 08:49:21
![](https://img.aspxhome.com/file/2023/7/72397_0s.png)
Java Web开发环境配置详解
2023-03-05 03:32:37
![](https://img.aspxhome.com/file/2023/1/66971_0s.png)
android中强制更新app实例代码
2023-05-23 18:42:34
![](https://img.aspxhome.com/file/2023/6/116006_0s.gif)
JAVA使用commos-fileupload实现文件上传与下载实例解析
2022-07-08 03:47:13
安卓(Android)开发之自定义饼状图
2022-04-06 11:13:11
![](https://img.aspxhome.com/file/2023/3/91033_0s.png)
C#中TextBox的横线样式及占位提示详解
2023-05-17 10:33:27
![](https://img.aspxhome.com/file/2023/8/113838_0s.png)
浅谈解决Hibernate懒加载的4种方式
2022-07-04 00:46:39
![](https://img.aspxhome.com/file/2023/0/85950_0s.png)
Java中List与Map初始化的一些写法分享
2021-11-23 13:13:46
IDEA 2022 中的Lombok 使用基础教程
2023-04-09 21:57:09
![](https://img.aspxhome.com/file/2023/7/64547_0s.png)
用Rational Rose逆向工程(java)生成类图(教程和错误解决)
2023-05-20 20:34:14
![](https://img.aspxhome.com/file/2023/3/90983_0s.gif)
详解Java后端优雅验证参数合法性
2021-09-06 16:07:22
![](https://img.aspxhome.com/file/2023/9/60159_0s.jpg)
在maven工程里运行java main方法
2023-09-18 04:33:20
C#设计模式实现之迭代器模式
2023-05-31 19:21:58
![](https://img.aspxhome.com/file/2023/8/82118_0s.png)
JavaMail入门教程之接收邮件(4)
2023-11-09 03:12:34
![](https://img.aspxhome.com/file/2023/1/80321_0s.jpg)
Java排序之冒泡排序的实现与优化
2023-11-10 21:35:56
![](https://img.aspxhome.com/file/2023/3/59033_0s.jpg)