使用XmlWriter寫Xml


使用XmlWriter寫Xml

 

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

 

C#處理Xml的相關隨筆:

 
 
 
 
 
 
 
 


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM