How To Add Or Change XML Encoding With .NET

Last time I wrote about comparing XML strings in .NET, and continuing that theme I’d like to discuss another handy XML trick.  As the last article pointed out, not all XML strings are created equal.  In fact, sometimes they are missing the necessary XML version and encoding information, like in the following sample XML:

<SampleXML>
    <SampleField1>Testing</SampleField1>
    <SampleField2 value="test value" />
    <SampleField3 type="int">456</SampleField3>
</SampleXML>

Notice anything missing?   Yes, the declaration information is not at the start of the XML string.  Declaration information like in the below example:

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

So I bet now you’re wondering how to fix such grievously flawed XML?  And how to it using .NET code, no less?  Fear not, dear reader, as I will show you that there is a way to get the necessary encoding information into the XML string without breaking a sweat.  Please take a look at the following code example:

string xmlStringValue =
    @"<SampleXML>
        <SampleField1>Testing</SampleField1>
        <SampleField2 value=""test value"" />
        <SampleField3 type=""int"">456</SampleField3>
    </SampleXML>";
Encoding encoding = Encoding.UTF8;

XmlDocument xmlDocument = new XmlDocument();
xmlDocument.LoadXml(xmlStringValue);

// Make sure that the XML does not already have an encoding.
if (xmlDocument.FirstChild.NodeType == XmlNodeType.XmlDeclaration)
{
    // If it does, set the top node to the correct encoding
    ((XmlDeclaration)xmlDocument.FirstChild).Encoding = encoding.HeaderName;
}
else
{
    // If not, we need to create an encoding node and set it to be the first part of the XML
    XmlDeclaration declarationNode = (XmlDeclaration)xmlDocument.CreateNode(XmlNodeType.XmlDeclaration, "xml", string.Empty);
    declarationNode.Encoding = encoding.HeaderName;
    xmlDocument.InsertBefore(declarationNode, xmlDocument.FirstChild);
}

// Now convert the XML document object back into a string
string xmlStringValueWithEncoding = xmlDocument.InnerXml;

I hope you were able to follow all of that.  What the code does is open the XML string as an XMLDocument object, and than proceeds to determine if it already has the XML declaration header line.  If it does already exist, the code simply swaps the encoding.  If not, the code creates a whole new XMLDeclaration object, sets the encoding with the Encoding object, and proceeds to insert that into the XML document.  The resulting XML string looks something like the following:

<?xml version="1.0" encoding="utf-8"?>
<SampleXML>
    <SampleField1>Testing</SampleField1>
    <SampleField2 value="test value" />
    <SampleField3 type="int">456</SampleField3>
</SampleXML>

Hopefully you find this code example useful for some of your XML needs.

Richard Franzen
Developer
ImageSource, Inc.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s