Sunday, December 27, 2009

XDocument / XElement - Save using a StringWriter as UTF-8 rather than UTF-16

One thing I find myself having to do on frequent projects is to save XML to a text file. I tend to use the .NET XDocument or XElement classes and use the Save() method. Once and a while I need to call Save() using a TextWriter. By default, when I do this the XML is written using a UTF-16 processing instruction as:

<?xml version="1.0" encoding="utf-16"?>

More often than not you want UTF-8; .NET XElement won't even read the UTF-16 contents back in using the Load() method (and things like IE won't be able to view it in the browser). To work around this problem I derive a new StringWriter and override the Encoding method:

public class StringWriterWithEncoding : StringWriter
{
private Encoding mEncoding;
public StringWriterWithEncoding(Encoding encoding)
{
mEncoding = encoding;
}

public override Encoding Encoding
{
get { return mEncoding; }
}
}

Now just use the new class and the XElement.Save() method to write as UTF-8:

XElement xml = XElement.Parse("<root/>");
using (StringWriterWithEncoding writer = new StringWriterWithEncoding(Encoding.UTF8))
{
xml.Save(writer);
Console.WriteLine(writer.ToString());
}

No comments:

Can't RDP? How to enable / disable virtual machine firewall for Azure VM

Oh no!  I accidentally blocked the RDP port on an Azure virtual machine which resulted in not being able to log into the VM anymore.  I did ...