~fhusson

XmlWriter and UTF-8 encoding without signature

I used this code to serialize some objects in Xml :

XmlWriter writer = new XmlTextWriter(stream, Encoding.UTF8);

But the output contains an UTF header, the Byte Order Mark (BOM). The use of the header/signature is usually for xml file, if you want to use the ouput in an HttpResponse, you don’t need the signature. (some parser can cause a parsing error in java, like org.xml.sax.SAXException).

Here is the change to remove the BOM :

XmlWriter writer = new XmlTextWriter(stream, new UTF8Encoding(false));
Discuss on Twitter