假定创建了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 玉开技术博客 http://www.cnblogs.com/yukaizhao ");

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;

完整的代码示例如下:

/*玉开技术博客 http://www.cnblogs.com/yukaizhao */
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 玉开技术博客 http://www.cnblogs.com/yukaizhao ");
 
                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();
 
                }
 
                //将xml内容输出到控制台中
                string xml = Encoding.UTF8.GetString(ms.ToArray());
                Console.WriteLine(xml);
            }
            Console.Read();
 
        }
    }
}

C#处理Xml的相关随笔:

C#操作Xml:使用XmlWriter写Xml的更多相关文章

  1. 使用XmlWriter写Xml

    假定创建了XmlWriter的实例变量xmlWriter,下文中将使用此实例变量写Xml 1.如何使用XmlWriter写Xml文档声明 ? // WriteStartDocument方法可以接受一个 ...

  2. c# 用XmlWriter写xml序列化

    using System.Text; using System.Xml; using System.Xml.Schema; using System.Xml.Serialization; using ...

  3. C#操作Xml:linq to xml操作XML

    LINQ to XML提供了更方便的读写xml方式.前几篇文章的评论中总有朋友提,你为啥不用linq to xml?现在到时候了,linq to xml出场了. .Net中的System.Xml.Li ...

  4. C#操作XML(读XML,写XML,更新,删除节点,与dataset结合等)【转载】

    已知有一个XML文件(bookstore.xml)如下: Corets, Eva 5.95 1.插入节点 往节点中插入一个节点: 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 ...

  5. 使用XmlWriter创建XML文件

    using System; using System.Collections.Generic; using System.Text; using System.IO; using System.Xml ...

  6. 使用dom4j写xml文件——源码

    1 dom4j下载与配置 1.1 dom4j下载 请移步下载链接 1.2 maven依赖 <dependency> <groupId>org.dom4j</groupId ...

  7. Oracle 远程访问配置 在 Windows Forms 和 WPF 应用中使用 FontAwesome 图标 C#反序列化XML异常:在 XML文档(0, 0)中有一个错误“缺少根元素” C#[Win32&WinCE&WM]应用程序只能运行一个实例:MutexHelper Decimal类型截取保留N位小数向上取, Decimal类型截取保留N位小数并且不进行四舍五入操作

    Oracle 远程访问配置   服务端配置 如果不想自己写,可以通过 Net Manager 来配置. 以下配置文件中的 localhost 改为 ip 地址,否则,远程不能访问. 1.网络监听配置 ...

  8. 【C++】【TinyXml】xml文件的读写功能使用——写xml文件

    TinyXml工具是常用比较简单的C++中xml读写的工具 需要加载 #include "TinyXml\tinyxml.h" 在TinyXML中,根据XML的各种元素来定义了一些 ...

  9. 2018-8-10-win10-uwp-读写XML

    title author date CreateTime categories win10 uwp 读写XML lindexi 2018-08-10 19:16:51 +0800 2018-2-13 ...

随机推荐

  1. iOS 8 新特性

    这篇文章会介绍iOS8开发相关的主要特性. App 插件 通过支持插件,iOS8让我们可以系统指定的区域进行扩展,也就是为用户的特定需求提供自定义的方法.例如:可以通过App插件帮助用户分享他们的内容 ...

  2. JavaScript对象(来自百度知道)

    JavaScript中对象的创建有以下几种方式: (1)使用内置对象(2)使用JSON符号(3)自定义对象构造 一.使用内置对象 JavaScript可用的内置对象可分为两种:1,JavaScript ...

  3. Xampp mysql无法启动的解决方案(转)

    如果出现mysql 无法启动表明在安装xampp 前已经安装了mysql,造成mysql服务无法启动. [mysql] MySQL Service detected with wrong path23 ...

  4. leetcode第一刷_Merge Sorted Array

    水题,只是思想还是实用的. 当然能够新建出一个数组.比較着拷贝过去.可是没有必要啊亲.想想为什么用源数组会麻烦,由于确定了前面的数.为了后面的数字不被覆盖,要依次往后移,转念一想,先确定后面的数字.就 ...

  5. poj3928 Ping pong 树状数组

    http://poj.org/problem?id=3928 Ping pong Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

  6. 【Java 之 JVM】Java内存结构概述

    watermark/2/text/aHR0cDovL2Jsb2cuY3Nkbi5uZXQvYWl3dXpoaWxpbmc=/font/5a6L5L2T/fontsize/400/fill/I0JBQk ...

  7. 开发一个Swing功能时的一点总结

    对JTextField进行效验,有两个途径:(1)是使用javax.swing.InputVerifier在获取焦点时进行校验(2)在点击“确定”按钮的监听事件中对控件的值进行校验 鉴于涉及的业务比较 ...

  8. 文章之间的基本总结:Activity生命周期

    孔子:温故而知新.它可以作为一个教师.<论语> 同样的学习技巧.对于技术文件或书籍的经典技术,期待再次看到它完全掌握,这基本上是不可能的,所以,我们常常回来几次,然后仔细研究,为了理解作者 ...

  9. 黑马程序猿——java基金会--jdk、变量

    学习内容: 1.Java发展历史 2.jdk和jre的差别,功能. 3.jdk和jre的下载和安装 4.配置环境.path和classpath 5.helloworld程序 6.进制之间的转换 7.凝 ...

  10. POJ 3126 Prime Path(BFS 数字处理)

    意甲冠军  给你两个4位质数a, b  每次你可以改变a个位数,但仍然需要素数的变化  乞讨a有多少次的能力,至少修改成b 基础的bfs  注意数的处理即可了  出队一个数  然后入队全部能够由这个素 ...