Search code examples
c#.netdatacontractserializer

DataContractSerializer deserialization of properties moved from derived class to base class


Using DCS I am trying to deserialize objects from XML where the object serialized is of type Child inheriting class Base where Child had some properties which were deserialized but were later moved to the class Base in code. Now these properties don't get deserialized:

Take a look at this XML:

<Base i:type="a:DirectoryEntry" xmlns="http://schemas.datacontract.org/2004/07/pending.Core.Models" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:a="http://schemas.datacontract.org/2004/07/pending.Core.Models.Directory">
    <Active>true</Active>
    <ContentType>DirectoryEntry</ContentType>
    <Created>2012-03-12T11:51:25.3401552+01:00</Created>
    <a:Location>location</a:Location>
    <a:OpenHours>opening</a:OpenHours>
</Base>

xmlns:a denotes the derived type name. This class used to hold those props. Now they were moved into Base class and they no longer get deserialized by DCS (properties are null after deserialization). I'm talking about the properties with a: in front of them (Location and OpenHours in this case).

We have a lot of these files holding data. What are my options to correctly deserialize those files with DCS?

please note that [KnownType] attributes are in use both on Base and Child class


Solution

  • My solution was to move properties back to the originating class and also leave them in the class that they were first moved to. It now gives the warning of Property hides the inherited member...Use the new keyword if hiding was intended but I can live with that because deserialization now works again we can move the data from one provider to another. I was unable to find any other solution and modification of all the serialized data files was not an option.