2
我在这里看到过几篇有关从某些数据源加载XML文档的文章,其中数据有Microsoft的专有UTF-8序言(例如,this one)。
但是,我找不到一个优雅的(和工作!)解决方案,它不涉及手动去除BOM字符。从字节数组(可选地包含BOM字符)中加载XMLDocument
例如,有this example:
byte[] b = System.IO.File.ReadAllBytes("c:\\temp_file_containing_bom.txt");
using (System.IO.MemoryStream oByteStream = new System.IO.MemoryStream(b)) {
using (System.Xml.XmlTextReader oRD = new System.Xml.XmlTextReader(oByteStream)) {
System.Xml.XmlDocument oDoc = new System.Xml.XmlDocument();
oDoc.Load(oRD);
Console.WriteLine(oDoc.OuterXml);
Console.ReadLine();
}
}
...但它仍不断抛出 “无效数据” 异常。
我的问题是,我有一个巨大的字节数组,其中有时包含BOM,有时它不。我需要在XMLDocument中加载它。而且我不相信我是需要照顾“帮手”字节的人。