简体   繁体   English

DataContractSerializer向后兼容

[英]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. 我正在尝试从旧版本的序列化对象中反序列化,但出现错误elementNameX_backField,这是不期望的,期望元素为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? 有什么想法,关于如何使它忽略newElement,如果它在已经序列化的文件中不存在?

Answer from the above comments for posterity: Adding DataContract attribute to ClassB fixed this issue. 以上评论的答案是:将DataContract属性添加到ClassB可以解决此问题。 Both XML serialization and DataContractSerialization work on that ClassB now XML序列化和DataContractSerialization现在都可以在该ClassB上运行

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM