Search code examples
c#xmldocument

XMLDocument.Load(), hexadecimal value 0x17, is an invalid character


I'm getting following error while loading an xml file using xmlDocument.Load() method:

I'm getting this error because I've some invalid character 'ETB' in my XML. How can I fix this? This character is a part of binary encoded image data.

<prompt>
   <![CDATA[<FlowDocument xmlns="http://schemas.microsoft.com/something"><Paragraph xmlns="http://schemas.microsoft.com/something"><InlineUIContainer BaselineAlignment="Center">
        <Image Stretch="None" Width="1024" Height="282"><Image.Source><BitmapImage><base64Binary>(Base 64 encoded data containing 'ETB' Text)
   </base64Binary></BitmapImage></Image.Source></Image></InlineUIContainer></Paragraph></FlowDocument>]]>
</prompt>

Base 64 Binary data is like this:

iVBORw0KGgoAAAANSUhEUgAABAAAAAEaCAIAAAAACrKOAAAAAXNSR0IArs4c6QAAAARnQU1BAACxjwv8YQUAAAAgY0hSTQAAeiYAAICEAAD6AAAAgOgAAHUwAADqYAAAOpgAABdwnLpRPAAAAAlwSFlzAAAOvwAADr8BOAVTJAAAaz9JREFUeF7tnQVgE03Tx8ODu7u7u7v7i7trcS0Ul+Luxd2Ke7FSrEix4oWixZ3iFErlnd1NLqlQ0sbukv+938eTnOzO/HavmdmdnY0WHByswgECIAACIAACIAACIAACIGAjBMgBwAECIAACIAACIAACIAACIGAjBFQ2oifUBAEQAAEQAAEQAAEQAAEQYOE/oAACIAACIAACIAACIAACIGA7BOAA2E5bQ1MQAAEQAAEQAAEQAAEQwAwA+gAIgAAIgAAIgAAIgAAI2BIBzADYUmtDVxAAARAAARAAARAAAZsnAAfA5rsAAIAACIAACIAACIAACNgSAeU5AEFRaZ4oPfSPikKVaYoqoqIqnpEBAXQGyzZCYJjqw56xrIS...

This data has an 'ETB' character in it.


Solution

  • This character is a part of binary encoded image data.

    Then it shouldn't be in an XML document. An XML document is text - typically if you want to include arbitrary binary data, you base64-encode it.

    This isn't actually a C# problem or a .NET problem - it's simply that your XML document is invalid.