Visual Studio 2008 doesn’t display XML files with the UTF-16 encoding
Recently, while working on a new tool for converting XML files using XSLT I have noticed something odd: Visual Studio (VS) 2008 wouldn’t display the contents of the converted XML files!
It’s not the first time that I’ve been working with XML and XSLT in .NET. I really like XML, and I use it for many purposes: from storing configuration data for applications to returning raw data so it can be presented using XSLT. XML is really really great. But what would keep Visual Studio 2008 from displaying the XML files I’ve converted using XSLT?
Then something hit me: would it be the UTF-16 encoding that makes VS2008 to display empty contents? As soon as I’ve changed the encoding to UTF-8 the contents of the XML files magically appeared in the source editor in Visual Studio. And I’m not talking about changing the physical encoding of the whole file: the only thing I’ve changed is the encoding string in the first line of the XML file!
By default all strings in the Windows OS are encoded using UTF-16. That is why the default string generated using XmlWriter is a UTF-16 string too. And while it seems like you can set the encoding using the XmlWriterSettings.Encoding property, the output will still be encoded in UTF-16. So how to deal with it?
Looking on the Internet for a solution I’ve stumbled upon an article by Ian Dykes on how to write UTF-8 XML using XmlTextWriter and StringWriter. Ian suggests creating an extend StringWriter class which allows you to set the encoding and use that class to instantiate XmlTextWriter. Using that approach will produce a XML file encoded in UTF-8 which will be opened by Visual Studio 2008 as expected. Little effort for a proper solution!