C#操作Xml:使用XmlWriter寫Xml

來源:互聯網
上載者:User

標籤:

假定建立了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");//寫CDataxmlWriter.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的相關隨筆:

1.通過XmlDocument讀寫Xml文檔2.使用XmlReader讀Xml,使用XmlWriter寫Xml3.使用Linq to xml存取XML4.通過XmlScheme定義固定格式xml文檔5.Xml序列化或者還原序列化類6.通過XPath尋找Xml節點7.通過Xslt轉化Xml格式

C#操作Xml:使用XmlWriter寫Xml

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.