Geeks With Blogs
INETA Pakistan Blogs Home of INETA Geeks

 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.

Hammad Rajjoub,
MVP (Windows Server System - XML Web Services),
User Group Leader - Dot Net Wizards (,
Chariman UG Relations Committee (,
Member Speakers Bureau (

Posted on Tuesday, August 23, 2005 1:54 PM | Back to top

Comments on this post: System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument

# re: System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument
Requesting Gravatar...
I took a look at your hack. Although a hack, it is pretty simple and seems to work. I am surprised that others might not see the benefit of using a string builder for writing xml and then displaying it in the browser.

I agree that either you should be able to set the encoding or IE should be able to display a utf-16 document. In my case, during testing and development, I would simply like to use Response.Write(xmlString) -- with xmlString being the XML returned from a web service call -- and display the result in the browser. For me, viewing the tree is often useful and sometimes sheds light on problems that you didn't know existed.

You saved me a bit of research.
Left by Oscar on Dec 08, 2005 5:43 AM

# re: System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument
Requesting Gravatar...
I can't comment on IE's ability to render/parse UTF-16 XML documents, because I haven't tried it.

But I can say that using a StringWriter to ultimately write content out to a file (or anything outside of your application's memory, e.g. as a web request over a network, etc.) is a flawed concept from the start. A StringWriter is supposed to be used just as its name implies: as a TextWriter object to write to a System.String. All System.String objects in .NET are represented in UTF-16 encoding. Every last one. A System.String object's encoding is fixed.

The minute you start talking about text data that's not in UTF-16 encoding, you're no longer talking about a System.String. And as such, you're no longer in the realm of using a StringWriter, which is designed exclusively for interoperation with System.String objects.

This is where Streams come into play. A Stream object is what you're looking for when you want to encode your text data into another encoding. All of the Stream-derived classes make it possible for you do this.

So, at the risk of repeating myself, StringWriter is a class to be used for in-memory manipulation of System.String data. That's it. Once it's time for that data to go somewhere outside of memory, you should be looking at other vehicles for that (most likely a Stream object).

Left by Kevin on Sep 29, 2006 4:35 AM

# re: System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument
Requesting Gravatar...
If you just do a simple string replacement of the utf-16 to utf-8 before writing to the file you will have both ie6 and vs2005 open it with no problem ... example ...

// File Save a settings file.
string xmlStateString = _clientState.XmlSerialize();
// strings in .Net are UTF-16 but we want the browser to be fooled
// to think it's a Utf-8 and open it...hack ....
xmlStateString = xmlStateString.Replace("utf-16", "utf-8");
StreamWriter fileStream = new StreamWriter(saveFileDialog1.OpenFile());

Left by Ehud Pardo on Nov 03, 2006 12:57 AM

# re: System.IO.StringWriter and UTF-16 Encoding problem with System.XML.XMLDocument
Requesting Gravatar...
using System.Text;
using System.Xml;

//Use a MemoryStream instead...
Encoding encoder = Encoding.GetEncoding("iso-8859-1");
XmlWriterSettings settings = new XmlWriterSettings();
settings.Encoding = encoder;
settings.Indent = true;
System.IO.MemoryStream txtWriter = new System.IO.MemoryStream();
XmlWriter writer = XmlWriter.Create(txtWriter, settings);
writer.WriteAttributeString("SomeAttribute", "SomeValue");
string result = encoder.GetString(txtWriter.GetBuffer());
Left by Nick on Aug 02, 2009 5:29 PM

# This worked for me
Requesting Gravatar...
Response.ContentEncoding = System.Text.Encoding.Unicode;

Left by ronin47 on Aug 08, 2012 3:57 PM

Your comment:
 (will show your gravatar)

Copyright © INETA Pakistan | Powered by: