Home  >  Article  >  Backend Development  >  Detailed introduction to the sample code for writing Xml using XmlWriter

Detailed introduction to the sample code for writing Xml using XmlWriter

黄舟
黄舟Original
2017-03-09 16:51:302534browse

Assume that the instance variable xmlWriter of XmlWriter is created. This instance variable will be used to write Xml below

1. How to use XmlWriter to write Xml document declaration

// WriteStartDocument方法可以接受一个bool参数(表示standalone,是否为独立文档)或者不指定参数standalone保持默认值
xmlWriter.WriteStartDocument(false|true);

Note that after using the WriteStartDocument method, it is best to call the xmlWrite.WriteEndDocument() method to close all possible unclosed tags
2. How to use XmlWriter to write xml nodes and attributes

//写节点
xmlWriter.WriteStartElement("cat");
//给节点添加属性
xmlWriter.WriteAttributeString("color", "white");
//给节点内部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();

Or write the xml node through the WriteElementString(string,string) method and write the node value at the same time, as follows

//通过WriteElementString可以添加一个节点同时添加节点内容
xmlWriter.WriteElementString("pig", "pig is great");

3. How to write CData

xmlWriter.WriteStartElement("dog");
//写CData
xmlWriter.WriteCData("<strong>dog is dog</strong>");
xmlWriter.WriteEndElement();

4. How to add comments using XmlWriter

xmlWriter.WriteComment("this is an example writed by 玉开技术博客 http://www.php.cn/ ");

5. How to set the output format of XmlWriter and solve the problem of outputting UTF-16
Set the xml output format, You need to pass the XmlWriterSettings class, the following code

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;


The complete code example is as follows:


/*玉开技术博客 http://www.php.cn/ */
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文件开始
                    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("dog is dog");
                    xmlWriter.WriteEndElement();

                    xmlWriter.WriteComment("this is an example writed by 玉开技术博客 http://www.php.cn/ ");

                    xmlWriter.WriteEndElement();
                    xmlWriter.WriteEndDocument();

                }

                //将xml内容输出到控制台中
                string xml = Encoding.UTF8.GetString(ms.ToArray());
                Console.WriteLine(xml);
            }
            Console.Read();

        }
    }
}



The above is the detailed content of Detailed introduction to the sample code for writing Xml using XmlWriter. For more information, please follow other related articles on the PHP Chinese website!

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn