XML Deserialization with invalid character

Recently I had a strange error, I serialized a complex object and during the deserialization process I got :
“Error : System.Xml.XmlException: ‘.’, hexadecimal value 0x00, is an invalid character. Line X, position Y.”
It appears that we had a “” inside a string, something like “HelloWorld” ! and during a classic serialization, the character was encoded in “”

With the help of Google I have found this post (2007) with a way to have an happy deserialization :-)

You can use XmlTextReader instead of XmlReader, but with more research I have found that you can still use XmlReader with XmlReaderSettings and CheckCharacters set to false.

Here is an example :

01
02
03
04
05
06
07
08
09
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
public class MyObject
{
    public string MyString { get; set; }
}
 
class Program
{
    static void Main(string[] args)
    {
         
        XmlSerializerFactory fact = new XmlSerializerFactory();
        XmlSerializer ser = fact.CreateSerializer(typeof(MyObject));
 
        MyObject obj0 = new MyObject();
        obj0.MyString = "HelloWorld";
 
        // Serialize the object
        StringWriter sw = new StringWriter();
        ser.Serialize(sw, obj0);
        string xml = sw.ToString();
        // We can check that in the xml a is transformed in
        Console.WriteLine(xml);
 
        // Classic use of XmlReader.Create
        StringReader sr1 = new StringReader(xml);
        XmlReader xr1 = XmlTextReader.Create(sr1); // xr1's type is XmlTextReaderImpl
        try
        {
            MyObject obj1 = (MyObject)ser.Deserialize(xr1);
            Console.WriteLine("XmlReader [CheckCharacters({0})] : Success : {1}", xr1.Settings.CheckCharacters, obj1.MyString);
            Console.WriteLine(obj1.MyString);
        }
        catch (Exception e)
        {
            Console.WriteLine("XmlReader [CheckCharacters({0})] : Error : {1}", xr1.Settings.CheckCharacters, e.InnerException);
        }
 
        // Using an XmlTextReader
        StringReader sr2 = new StringReader(xml);
        XmlTextReader xr2 = new XmlTextReader(sr2);
        // xr2.Settings is null
        MyObject obj2 = (MyObject)ser.Deserialize(xr2);
        Console.WriteLine("XmlTextReader : Success : {0}", obj2.MyString);
 
        // Using XmlReader with the good XmlReaderSettings
        StringReader sr3 = new StringReader(xml);
        XmlReaderSettings settings = new XmlReaderSettings();
        settings.CheckCharacters = false; // default value is true;
        XmlReader xr3 = XmlTextReader.Create(sr3, settings); // xr3.Settings.CheckCharacters is a read only and xr3's type is XmlTextReaderImpl
        MyObject obj3 = (MyObject)ser.Deserialize(xr3);
        Console.WriteLine("XmlReader [CheckCharacters({0})] : Success : {1}", xr3.Settings.CheckCharacters, obj3.MyString);
    }
}
Advertisements

2 thoughts on “XML Deserialization with invalid character

  1. Pingback: Serialize and Deserialize POCO classes in XML : Harsh Baid

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 )

Google+ photo

You are commenting using your Google+ 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 )

Connecting to %s