Search code examples
c#wcfdatacontractserializer

DataContractSerializer backward compatibility


I am trying to deserialize from an older version of a serialized object, and I get an error elementNameX_backField is not expected, expecting element newElementName_backingField. Here is how my code looks like:

using (var fileStream = new FileStream(fullName, FileMode.Open))
                using (var decryptStream = new CryptoStream(fileStream, decryptor, CryptoStreamMode.Read))
                using (var reader = XmlDictionaryReader.CreateBinaryReader(decryptStream, XmlDictionaryReaderQuotas.Max))
                {

                    var ser = new DataContractSerializer(typeof(classA), KnownTypes);
                    rv = ser.ReadObject(reader) as classA;
                }
[DataContract]
public class classA
{
 [DataMember]
 public classB ClassBee {get; set;}
}
[Serializable]
public class ClassB
{
[XmlElement("element")]
public customType1 elementNameX{get; set;}
[XmlElement("newElement")]
public customType2 newElementName{get; set;}
}

Any ideas, on how I could make it ignore newElement if it doesn't exist in the already serialized file?


Solution

  • Answer from the above comments for posterity: Adding DataContract attribute to ClassB fixed this issue. Both XML serialization and DataContractSerialization work on that ClassB now