0

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;
        }
meetjaydeep
  • 1,752
  • 4
  • 25
  • 39
  • 1
    Where is the real xml? where is the definition of *Employee* class? where is your code? – I4V Sep 10 '13 at 13:12
  • It says that the element it has found (unexpectedly) includes a namespace... are you sure that is the actual xml? there's no `xmlns` all over it? – Marc Gravell Sep 10 '13 at 13:15

0 Answers0