Monday, August 22, 2005

System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument

I am continuing my work with System.XML and System.XML.XPath namespaces. This implies that my adventure with XML and .NET is not over yet. I came across this interesting situation while freezing my code for next release.

I was actually generating XML document using System.IO.StringWriter and System.XML.XMLDocument. Apparently there shouldnt be any issues with it. But actually there is a small problem with that. When you use StringWriter to write XMLDocuments it appends a prologue with “UTF-16” encoding specified. Now theres a big problem if you want to change the encoding to “UTF-8” or any thing else (available in System.Text.Encoding). You cant really do that... since there is no provision for it. Strange as it may sound, I came across a hack for this situation as well located at . And then i came across a reason why XMLDocument generated from StringWriter has to be encoded using UTF-16 encoding.

Well the strange most part of this adventure was that IE 6.0 din't support viewing utf-16 encoded strings. Heck!. While Mozilla Firefox was graceful enough to show tree view of utf-16 encoded XML document.

So here are the two things that I as a developer would have liked to see in .NET and IE.

1- There should have been some way provided to change the encoding scheme, yes even using System.StringWriter. I believe this thing could have been supported through System.XML.XMLDocument constructor.

2-IE should have supported viewing UTF-16 document.

No comments: