使用XmlWriter寫Xml

y_keven發表於2014-01-08

假定建立了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();
 
        }
    }
}

相關文章