Using [XmlAnyElement]

前端 未结 3 1875
情话喂你
情话喂你 2021-01-20 19:07

I\'m trying to preserve data on two different versions of an object and not having any success with it. Can anyone tell me what I\'m doing wrong?

Version One of the

3条回答
  •  既然无缘
    2021-01-20 19:45

    To preserve unknown elements of future or past versions of DataContracts, you can implement the IExtensibleDataObject interface. Doing so will cause any unknown elements to be placed in a property called ExtensionData which allows future re-serialization without missing data.

    Example usage would be:

    [DataContract(Name="Person")]
    public class Person_V1 : IExtensibleDataObject
    {
        [DataMember(Name = "Name")]
        public string Name;
    
        [DataMember(Name = "Age")]
        public int Age;
    
        public ExtensionDataObject ExtensionData { get; set; }
    }
    

    When the Person_V2 object is deserialized to a Person_V1 object, the Weight property is stored in ExtensionData, and is returned to the serialized stream when it is re-serialized.

提交回复
热议问题