Whaly's World tail -f /var/log/whaly

16Jul/092

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));
Comments (2) Trackbacks (1)
  1. Thanks man, I read numerous other pages with all sorts of explanations on what to do but no example, yours was much easier and quicker :)

  2. Awesome! Just what I was after. Concise and to the point explanation of how to do it. Thanks.


Leave a comment

(required)