程序師世界是廣大編程愛好者互助、分享、學習的平台,程序師世界有你更精彩!
首頁
編程語言
C語言|JAVA編程
Python編程
網頁編程
ASP編程|PHP編程
JSP編程
數據庫知識
MYSQL數據庫|SqlServer數據庫
Oracle數據庫|DB2數據庫
 程式師世界 >> 編程語言 >> .NET網頁編程 >> C# >> C#入門知識 >> 使用XmlWriter寫Xml

使用XmlWriter寫Xml

編輯:C#入門知識

假定創建了XmlWriter的實例變量xmlWriter,下文中將使用此實例變量寫Xml

1.如何使用XmlWriter寫Xml文檔聲明

view sourceprint?// WriteStartDocument方法可以接受一個bool參數(表示standalone,是否為獨立文檔)或者不指定參數standalone保持默認值 

xmlWriter.WriteStartDocument(false|true);

注意在使用WriteStartDocument方法後最好調用xmlWrite.WriteEndDocument()方法來關閉所有可能未關閉標簽
2.如何使用XmlWriter寫xml節點以及屬性

view sourceprint?//寫節點 

xmlWriter.WriteStartElement("cat"); 

//給節點添加屬性 

xmlWriter.WriteAttributeString("color", "white"); 

//給節點內部添加文本 

xmlWriter.WriteString("I'm a cat"); 

xmlWriter.WriteEndElement();

或者通過WriteElementString(string,string)方法寫xml節點同時寫下節點值,如下

view sourceprint?//通過WriteElementString可以添加一個節點同時添加節點內容 

xmlWriter.WriteElementString("pig", "pig is great");

3.如何寫CData

view sourceprint?xmlWriter.WriteStartElement("dog"); 

//寫CData 

xmlWriter.WriteCData("<strong>dog is dog</strong>"); 

xmlWriter.WriteEndElement();

4.如何使用XmlWriter添加注釋

view sourceprint?xmlWriter.WriteComment("this is an example writed by 玉開技術博客 http://www.cnblogs.com/yukaizhao ");

5.如何設置XmlWriter的輸出格式,解決輸出UTF-16問題
設置xml輸出格式,需要通過XmlWriterSettings類,如下代碼

view sourceprint?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;


完整的代碼示例如下:


view sourceprint?/*玉開技術博客 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(); 

  

        } 

    } 

}

  1. 上一頁:
  2. 下一頁:
Copyright © 程式師世界 All Rights Reserved