0
我有一個System.Xml.XmlDocument對象,我想將xml編碼從UT16更改爲UTF8。我該怎麼做?指定XMLDoc上的編碼格式
我有一個System.Xml.XmlDocument對象,我想將xml編碼從UT16更改爲UTF8。我該怎麼做?指定XMLDoc上的編碼格式
XmlDocument使用它自己的(基於DOM的)XML的內部表示;編碼僅在XML寫入並作爲文本存儲在某處時才起作用。您可以使用XmlDocument.WriteTo方法,並提供XmlWriter
,使用傳入XmlWriter.Create
的XmlWriterSettings
進行配置。有一個XmlWriterSettings.Encoding屬性,您可以在其中指定UTF8。
例如:
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Encoding = Encoding.UTF8;
using (XmlWriter xmlWriter = XmlWriter.Create(filename, xmlWriterSettings))
{
XmlDocument.WriteTo(xmlWriter);
}
的問題是沒有意義的。 UTF-8是默認的。你真的試圖加載一個.xml並將其轉換嗎? – 2010-06-15 12:04:08