IXmlSerializable and inheritance

Let's say that I have two classes, a base class and a derived class. They are fairly simple classes, and mainly just act as data structures (with the derived class obviously being slightly more complex).

public class BaseUserSession
    // ... Various properties ...

public class DerivedUserSession : BaseUserSession
    // ... Even more properties ...

These classes need to be serialized into XML. Now, this can be done simply by specifying [Serializable] above the class declarations, and the default XML serializer works well.

In reality, however, there are multiple classes that inherit from the base class. My goal is to be able to have the serialized XML of any of the derived classes be consumable by the base class.

I have tried two methods, neither of which work currently. First, using the native XML serializer and some control attributes within the base class. The generated XML looks something like this:

<?xml version="1.0" encoding="utf-16"?>
<DerivedUserSession xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:SSO="www.mywebsite.com">
    <SSO:Id />
    <SSO:Role />
    <EmailAddress />
    <FullName />
    <Street />
    <City />
    <State />
    <Zip />
    <DayPhone />
    <EveningPhone />
    <ErrorMessage />

The tags that are prefixed with SSO are the ones that were inherited from the base class. But when I attempt to deserialize this into a BaseUserSession object, it throws an exception: System.InvalidOperationException: <DerivedUserSession xmlns=''> was not expected.

I also tried making the base class IXmlSerializable, and reading and writing all of the XML manually. However, the derived classes fail to write out their properties, and I really don't want to implement IXmlSerializable for every class.

Is there an easier way to do this?


I have finally implemented a fairly ugly method to accomplish this. The two classes inherit from the base, and they all implement IXmlSerializable. Each class (including the base) writes out its field values (painstakingly...) into its own XML hierarchy using reflection, as well as reads those values back. The first values written are always from the base class.

Each application also includes code that manages the serialization into XML and back, providing an easy interface. The session itself doesn't do anything more now than simply accept the serialized XML and store it under a common key. The serialization into and from XML is all handled by the application, and a common XmlRootAttribute is used in all applications to bypass the InvalidOperationException.

So now, when another application tries to deserialize XML that was initially created in a different application, it manages to read through the common base class values before it hits an unrecognized element, at which point it goes and fetches the needed values elsewhere, which is possible because it will already have the common base values.

I need to read up on serialization principles...


XmlSerialization is not polymorphic (the member attributes, however, go with the declaring class; i.e. unless you overrule a member with the new keyword (in contrast to override) you'll 'inherit' the serialization attributes.

Deserialization always results in the actual type that was serialized. This makes a lot of sense, really.

Need Your Help

Make NavigationBar Title same as table cell chosen in previous view

iphone ios xcode xcode4 ios5

I am trying to learn iPhone development as I've read many others on here are also saying. And Ive searched through about 20 pages of previous threads but couldn't quite find one that is what I need...

How can I send an incorrect Content-Length header for an HTTP request using Perl?

perl http http-headers cpan content-length

I'm trying to debug a weird warning that is showing up in server logs when a Plack::Request is being parsed. In some cases, a broken UserAgent will send a Content-Length header that looks somethin...

About UNIX Resources Network

Original, collect and organize Developers related documents, information and materials, contains jQuery, Html, CSS, MySQL, .NET, ASP.NET, SQL, objective-c, iPhone, Ruby on Rails, C, SQL Server, Ruby, Arrays, Regex, ASP.NET MVC, WPF, XML, Ajax, DataBase, and so on.