Is there any way to output the contents of an XDocument without the BOM? When reading the output with Flash, it causes errors.
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'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);
Would create an XmlWriter with UTF-8 encoding and without the Byte Order Mark.
Slight mod to Chris Wenham's answer.
You can't modify the encoding once the XmlWriter is created, but you can set it using the XmlWriterSettings when creating the XmlWriter
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = new System.Text.UTF8Encoding(false);
XmlWriter writer = XmlWriter.Create("foo.xml", settings);
myXDocument.WriteTo(writer);
I couldn't add a comment above, but if anyone uses Chris Wenham's suggestion, remember to Dispose of the writer! I spent some time wondering why my output was truncated, and that was the reason.
Suggest a using(XmlWriter...) {...}
change to Chris' suggestion
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With