基于C#中XmlWriter寫入Xml的深入分析
更新時間:2013年05月20日 08:56:31 作者:
本篇文章是對C#中XmlWriter寫入Xml進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下
假定創(chuàng)建了XmlWriter的實例變量xmlWriter,下文中將使用此實例變量寫Xml
1.如何使用XmlWriter寫Xml文檔聲明
// WriteStartDocument方法可以接受一個bool參數(shù)(表示standalone,是否為獨立文檔)或者不指定參數(shù)standalone保持默認(rèn)值
xmlWriter.WriteStartDocument(false|true);
注意在使用WriteStartDocument方法后最好調(diào)用xmlWrite.WriteEndDocument()方法來關(guān)閉所有可能未關(guān)閉標(biāo)簽
2.如何使用XmlWriter寫xml節(jié)點以及屬性
//寫節(jié)點
xmlWriter.WriteStartElement("cat");
//給節(jié)點添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
或者通過WriteElementString(string,string)方法寫xml節(jié)點同時寫下節(jié)點值,如下
//通過WriteElementString可以添加一個節(jié)點同時添加節(jié)點內(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個字節(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個字節(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é)點
xmlWriter.WriteStartElement("root");
//寫字節(jié)點
xmlWriter.WriteStartElement("cat");
//給節(jié)點添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
//通過WriteElementString可以添加一個節(jié)點同時添加節(jié)點內(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)容輸出到控制臺中
string xml = Encoding.UTF8.GetString(ms.ToArray());
Console.WriteLine(xml);
}
Console.Read();
}
}
}
1.如何使用XmlWriter寫Xml文檔聲明
復(fù)制代碼 代碼如下:
// WriteStartDocument方法可以接受一個bool參數(shù)(表示standalone,是否為獨立文檔)或者不指定參數(shù)standalone保持默認(rèn)值
xmlWriter.WriteStartDocument(false|true);
注意在使用WriteStartDocument方法后最好調(diào)用xmlWrite.WriteEndDocument()方法來關(guān)閉所有可能未關(guān)閉標(biāo)簽
2.如何使用XmlWriter寫xml節(jié)點以及屬性
復(fù)制代碼 代碼如下:
//寫節(jié)點
xmlWriter.WriteStartElement("cat");
//給節(jié)點添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
或者通過WriteElementString(string,string)方法寫xml節(jié)點同時寫下節(jié)點值,如下
復(fù)制代碼 代碼如下:
//通過WriteElementString可以添加一個節(jié)點同時添加節(jié)點內(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個字節(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個字節(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é)點
xmlWriter.WriteStartElement("root");
//寫字節(jié)點
xmlWriter.WriteStartElement("cat");
//給節(jié)點添加屬性
xmlWriter.WriteAttributeString("color", "white");
//給節(jié)點內(nèi)部添加文本
xmlWriter.WriteString("I'm a cat");
xmlWriter.WriteEndElement();
//通過WriteElementString可以添加一個節(jié)點同時添加節(jié)點內(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)容輸出到控制臺中
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ì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧2018-09-09

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