好吧,我正在嘗試使用UTF8文本文件。我一直在努力爭取作者爲UTF8而投入的BOF字符,這使得我需要用到的任何東西來閱讀包括序列化程序和其他文本閱讀器的文件。UTF8文件字符的開頭正在破壞串行器和閱讀器
我得到一個領先的六個字節的數據:
0xEF
0xBB
0xBF
0xEF
0xBB
0xBF
(現在我看着它,我意識到有兩個字符存在該UTF8 BOF標誌我是雙編碼?它)?
注意串行器編碼爲UTF8,然後內存流得到一個字符串作爲UTF8,然後我寫入UTF8文件的字符串......似乎很多冗餘。思考?
//I'm storing this xml result to a database field. (this one includes the BOF chars)
using (MemoryStream ms = new MemoryStream())
{
Utility.SerializeXml(ms, root);
xml = Encoding.UTF8.GetString(ms.ToArray());
}
//later on, I would take that xml and then write it out to a file like this:
File.WriteAllText(path, xml, Encoding.UTF8);
public static void SerializeXml(Stream output, object data)
{
XmlSerializer xs = new XmlSerializer(data.GetType());
XmlWriterSettings settings = new XmlWriterSettings();
settings.Indent = true;
settings.IndentChars = "\t";
settings.Encoding = Encoding.UTF8;
XmlWriter writer = XmlTextWriter.Create(output, settings);
xs.Serialize(writer, data);
writer.Flush();
writer.Close();
}
完美!這回答了我的問題。我能夠在沒有BOM的情況下編寫文件。我用'UTF8Encoding(false)'用'Encoding.UTF8'替換了所有的位置。 – Nathan 2009-11-23 20:54:28