I am using NetDataContractSerializer
for serialization/deserialization.
I have serialized the dictionary which contains the Employee object
Now I have changed Employee object and included boolean field IsManager
So, after deserialzing above xml I want Employee object with IsManger as false value.
But I am getting below error while deserializing the xml.
Error in line 1 position 1676. 'Element' 'x003C....' from namespace 'http://schemas.datacontract.org/2004/07/..' is not expected. Expecting element 'x003C....'.
I have tried [DataMember(IsRequired = false)]
as well as [XmlElement(IsNullable=true)]
attribute for IsManager field but it didn't solve the problem.
Employee Class
public Class Employee
{
public string FirstName{get;set;}
public string LastName{get;set;}
public bool IsManager{get;set;}
}
Employee class is serialized in the dictionary to read it from dictionary(xml) I am using below code
internal static IDictionary<TKey, TValue> DeserializeData<TKey, TValue>(string xml) where TValue : class
{
if (xml == null || xml.Equals(string.Empty))
{
return null;
}
IDictionary<TKey, TValue> dictionary = null;
var deserializer = new NetDataContractSerializer
{
AssemblyFormat = FormatterAssemblyStyle.Simple
};
var sr = new StringReader(xml);
using (XmlReader reader = XmlReader.Create(sr))
{
try
{
dictionary = deserializer.ReadObject(reader) as IDictionary<TKey, TValue>;
}
catch (Exception e)
{
Trace.WriteLine(
string.Format(
CultureInfo.InvariantCulture, "Exception during de-serialization of data: {0}", e.Message));
}
}
return dictionary;
}