C # Operations XML: Using XmlWriter to write XML

Source: Internet
Author: User

Assume that you created an instance variable XmlWriter for XmlWriter, and you will use this instance variable to write the XML

1. How to write an XML document declaration using XmlWriter

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

Note It is best to call the Xmlwrite.writeenddocument () method after you use the WriteStartDocument method to close all labels that may not be closed
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 an XML node with the WriteElementString (string,string) method and write down the node value, as follows

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

3. How to write CDATA

?
xmlWriter.WriteStartElement("dog");//写CDataxmlWriter.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.cnblogs.com/yukaizhao ");

5. How to set the output format of XmlWriter to solve the problem of output UTF-16
To set the XML output format, you need to pass the XmlWriterSettings class with the following code

?
XmlWriterSettings settings = newXmlWriterSettings();//要求缩进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.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 # Related essays on XML processing:

1. Read and write XML documents via XmlDocument 2. Use XmlReader to read XML and write XML3 using XmlWriter. Using LINQ to XML Access XML4. Defines a fixed-format XML document 5.XML serialization or deserialization by Xmlscheme 6. Finding XML nodes through XPath 7. Transforming XML Formats through XSLT

C # Operations XML: Using XmlWriter to write XML

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.