Monday, January 23, 2012

DataContractSerializer Error in Deserialization : There was an error deserializing the object of type The maximum nametable character count quota (16384) has been exceeded while reading XML data.

Below is an example of Datacontract deserialization of an object

 public static void ReadObject(ClassTest tstObj)
         {
             FileStream fs = new FileStream(@"D:\" + tstObj.Name + ".xml", FileMode.Open);

             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(fs, new XmlDictionaryReaderQuotas());
            
             DataContractSerializer ser = new DataContractSerializer(typeof(ClassTest ));

             // Deserialize the data and read it from the instance.
             ClassTest deserializedTest =
                 (ClassTest )ser.ReadObject(reader, true);
             reader.Close();
             fs.Close();
            
         }

To overcome the error just change the highlighted line to below:

XmlDictionaryReaderQuotas dictQuota = new XmlDictionaryReaderQuotas();
             dictQuota.MaxNameTableCharCount = int.MaxValue;
             dictQuota.MaxArrayLength = int.MaxValue;
             dictQuota.MaxStringContentLength = int.MaxValue;
             XmlDictionaryReader reader =
                 XmlDictionaryReader.CreateTextReader(writer, dictQuota);


No comments:

Post a Comment