问题的XDocument和BOM(字节顺序标记)字节、标记、顺序、问题

2023-09-03 17:02:13 作者:愚昧

有没有什么办法可以输出一个XDocument未经BOM的内容是什么?当读取与Flash输出,它会导致错误。

Is there any way to output the contents of an XDocument without the BOM? When reading the output with Flash, it causes errors.

推荐答案

如果你正在编写XML用的XmlWriter,可以将编码设置为一个已经初始化离开了BOM。

If you're writing the XML with an XmlWriter, you can set the Encoding to one that has been initialized to leave out the BOM.

EG:System.Text.UTF8Encoding的构造函数接受一个布尔值,指定是否要在BOM,这样:

EG: System.Text.UTF8Encoding's constructor takes a boolean to specify whether you want the BOM, so:

XmlWriter writer = XmlWriter.Create("foo.xml");
writer.Settings.Encoding = new System.Text.UTF8Encoding(false);
myXDocument.WriteTo(writer);

会造成使用UTF-8编码,没有字节顺序标记的XmlWriter的。

Would create an XmlWriter with UTF-8 encoding and without the Byte Order Mark.