基于C#中XmlWriter寫入Xml的深入分析
更新時(shí)間:2013年05月20日 08:56:31 作者:
本篇文章是對(duì)C#中XmlWriter寫入Xml進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
假定創(chuàng)建了XmlWriter的實(shí)例變量xmlWriter,下文中將使用此實(shí)例變量寫Xml
1.如何使用XmlWriter寫Xml文檔聲明
// WriteStartDocument方法可以接受一個(gè)bool參數(shù)(表示standalone,是否為獨(dú)立文檔)或者不指定參數(shù)standalone保持默認(rèn)值
xmlWriter.WriteStartDocument(false|true);
注意在使用WriteStartDocument方法后最好調(diào)用xmlWrite.WriteEndDocument()方法來關(guān)閉所有可能未關(guān)閉標(biāo)簽
2.如何使用XmlWriter寫xml節(jié)點(diǎn)以及屬性
//寫節(jié)點(diǎn)
xmlWriter.WriteStartElement("cat");
//給節(jié)點(diǎn)添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點(diǎn)內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
或者通過WriteElementString(string,string)方法寫xml節(jié)點(diǎn)同時(shí)寫下節(jié)點(diǎn)值,如下
//通過WriteElementString可以添加一個(gè)節(jié)點(diǎn)同時(shí)添加節(jié)點(diǎn)內(nèi)容
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.dbjr.com.cn ");
5.如何設(shè)置XmlWriter的輸出格式,解決輸出UTF-16問題
設(shè)置xml輸出格式,需要通過XmlWriterSettings類,如下代碼
XmlWriterSettings settings = new XmlWriterSettings();
//要求縮進(jìn)
settings.Indent = true;
//注意如果不設(shè)置encoding默認(rèn)將輸出utf-16
//注意這兒不能直接用Encoding.UTF8如果用Encoding.UTF8將在輸出文本的最前面添加4個(gè)字節(jié)的非xml內(nèi)容
settings.Encoding = new UTF8Encoding(false);
//設(shè)置換行符
settings.NewLineChars = Environment.NewLine;
完整的代碼示例如下:
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();
//要求縮進(jìn)
settings.Indent = true;
//注意如果不設(shè)置encoding默認(rèn)將輸出utf-16
//注意這兒不能直接用Encoding.UTF8如果用Encoding.UTF8將在輸出文本的最前面添加4個(gè)字節(jié)的非xml內(nèi)容
settings.Encoding = new UTF8Encoding(false);
//設(shè)置換行符
settings.NewLineChars = Environment.NewLine;
using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
{
//寫xml文件開始<?xml version="1.0" encoding="utf-8" ?>
xmlWriter.WriteStartDocument(false);
//寫根節(jié)點(diǎn)
xmlWriter.WriteStartElement("root");
//寫字節(jié)點(diǎn)
xmlWriter.WriteStartElement("cat");
//給節(jié)點(diǎn)添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點(diǎn)內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
//通過WriteElementString可以添加一個(gè)節(jié)點(diǎn)同時(shí)添加節(jié)點(diǎn)內(nèi)容
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.dbjr.com.cn ");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
//將xml內(nèi)容輸出到控制臺(tái)中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
}
}
1.如何使用XmlWriter寫Xml文檔聲明
復(fù)制代碼 代碼如下:
// WriteStartDocument方法可以接受一個(gè)bool參數(shù)(表示standalone,是否為獨(dú)立文檔)或者不指定參數(shù)standalone保持默認(rèn)值
xmlWriter.WriteStartDocument(false|true);
注意在使用WriteStartDocument方法后最好調(diào)用xmlWrite.WriteEndDocument()方法來關(guān)閉所有可能未關(guān)閉標(biāo)簽
2.如何使用XmlWriter寫xml節(jié)點(diǎn)以及屬性
復(fù)制代碼 代碼如下:
//寫節(jié)點(diǎn)
xmlWriter.WriteStartElement("cat");
//給節(jié)點(diǎn)添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點(diǎn)內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
或者通過WriteElementString(string,string)方法寫xml節(jié)點(diǎn)同時(shí)寫下節(jié)點(diǎn)值,如下
復(fù)制代碼 代碼如下:
//通過WriteElementString可以添加一個(gè)節(jié)點(diǎn)同時(shí)添加節(jié)點(diǎn)內(nèi)容
xmlWriter.WriteElementString("pig", "pig is great");
3.如何寫CData
復(fù)制代碼 代碼如下:
xmlWriter.WriteStartElement("dog");
//寫CData
xmlWriter.WriteCData("<strong>dog is dog</strong>");
xmlWriter.WriteEndElement();
4.如何使用XmlWriter添加注釋
復(fù)制代碼 代碼如下:
xmlWriter.WriteComment("this is an example writed by http://www.dbjr.com.cn ");
5.如何設(shè)置XmlWriter的輸出格式,解決輸出UTF-16問題
設(shè)置xml輸出格式,需要通過XmlWriterSettings類,如下代碼
復(fù)制代碼 代碼如下:
XmlWriterSettings settings = new XmlWriterSettings();
//要求縮進(jìn)
settings.Indent = true;
//注意如果不設(shè)置encoding默認(rèn)將輸出utf-16
//注意這兒不能直接用Encoding.UTF8如果用Encoding.UTF8將在輸出文本的最前面添加4個(gè)字節(jié)的非xml內(nèi)容
settings.Encoding = new UTF8Encoding(false);
//設(shè)置換行符
settings.NewLineChars = Environment.NewLine;
完整的代碼示例如下:
復(fù)制代碼 代碼如下:
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();
//要求縮進(jìn)
settings.Indent = true;
//注意如果不設(shè)置encoding默認(rèn)將輸出utf-16
//注意這兒不能直接用Encoding.UTF8如果用Encoding.UTF8將在輸出文本的最前面添加4個(gè)字節(jié)的非xml內(nèi)容
settings.Encoding = new UTF8Encoding(false);
//設(shè)置換行符
settings.NewLineChars = Environment.NewLine;
using (XmlWriter xmlWriter = XmlWriter.Create(ms, settings))
{
//寫xml文件開始<?xml version="1.0" encoding="utf-8" ?>
xmlWriter.WriteStartDocument(false);
//寫根節(jié)點(diǎn)
xmlWriter.WriteStartElement("root");
//寫字節(jié)點(diǎn)
xmlWriter.WriteStartElement("cat");
//給節(jié)點(diǎn)添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點(diǎn)內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
//通過WriteElementString可以添加一個(gè)節(jié)點(diǎn)同時(shí)添加節(jié)點(diǎn)內(nèi)容
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.dbjr.com.cn ");
xmlWriter.WriteEndElement();
xmlWriter.WriteEndDocument();
}
//將xml內(nèi)容輸出到控制臺(tái)中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
}
}
相關(guān)文章
c#基礎(chǔ)系列之System.String的深入理解
這篇文章主要給大家介紹了關(guān)于c#基礎(chǔ)系列之System.String的深入理解,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09.NET/C# 使用Stopwatch測量運(yùn)行時(shí)間
這篇文章主要介紹了.NET/C# 使用Stopwatch測量運(yùn)行時(shí)間,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2020-01-01Unity實(shí)現(xiàn)弧形移動(dòng)效果
這篇文章主要為大家詳細(xì)介紹了Unity實(shí)現(xiàn)弧形移動(dòng)效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2020-06-06C#訪問SqlServer設(shè)置鏈接超時(shí)的方法
這篇文章主要介紹了C#訪問SqlServer設(shè)置鏈接超時(shí)的方法,涉及CommandTimeout屬性的相關(guān)設(shè)置技巧,非常簡單實(shí)用,需要的朋友可以參考下2015-06-06

一款域名監(jiān)控小工具 Domain(IP)Watcher 實(shí)現(xiàn)代碼
域名是否正常,網(wǎng)站是否可以正常訪問是很頭痛的問題,怎樣簡單地監(jiān)控域名是否可以正常訪問呢,這里發(fā)布一款域名監(jiān)控小工具:Domain(IP)Watcher
2011-11-11