使用XmlWriter写Xml

2018-06-23 23:59:11来源:未知 阅读 ()

新老客户大回馈,云服务器低至5折

使用XmlWriter写Xml

假定创建了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.baidu.com/");

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

C#处理Xml的相关随笔:

1.通过XmlDocument读写Xml文档
2.使用XmlReader读Xml,使用XmlWriter写Xml
3.使用Linq to xml存取XML
4.通过XmlScheme定义固定格式xml文档
5.Xml序列化或者反序列化类
6.通过XPath查找Xml节点
7.通过Xslt转化Xml格式

标签:

版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有

上一篇:如何在 Azure 中创建 ASP.NET Web 应用

下一篇:Sql Server数据库使用触发器和sqlbulkcopy大批量数据插入更新