Thursday, October 1, 2009

DOT.NET - Make invalid UTF8 charatcters available in XML

To change my posts a little lets talk about .NET, not WPF, a .NET little trick I discovered when creating UTF8 enabled XML files.

If you want to enable UTF8 files through an editor you would have to do the following:

<?xml version="1.0" encoding="ISO-8859-1"?>

And to do the same when you use a XmlWriter (that's the trick), you first have to set the Encoding of the XmlWriter and then you just have to force it to write the start document element.

XmlTextWriter objWriter = new XmlTextWriter(sPath, Encoding.UTF8);
objWriter.WriteStartDocument();

Hope it helps!