Home All Groups Group Topic Archive Search About

XmlSerializer usage

Author
22 Aug 2006 2:29 PM
David Jessee
1) I have an XSD that defines a document structure 
2)  I have a business object whose internal state can be represented by an
XML document that is conformant to the XSD specification

Many times, the first thought a lot of folks have is, "Silly, just run your
XSD through xsd.exe and you can get a dataset or a class model!"  That's not
going to work for me, though.  My business object contains a good bit of
logic and the lightweight zero-method classes that are generated by xsd.exe
are not appropriate for what I'm doing. 

So, I decided to take the approach of implementing the IXmlSerializable
interface instead.  Life is good. I'm curious, though, how folks insulate
their business object from changes in the schema?  This object's state is
being persisted as an XML string.  There will come a time when I have to
change the XSD to encance the feature set.  So, my class will have to be able
to Deserialize its state from either of 2 (or more) different XSD Schemas
because there may be older persisted versions (and yes, I want to validate
the XML).

How do people generally handle this scenario? (no...just not validating the
XML is not an option)


Here's my approach:
If the Class is called Person, then

The Class Person Implements the IXmlSerializable interface
The Person class has a number of private nested classes (so they have access
to the Person class's state fields).  These are called

DeSerializationFactory
Version1Deserializer
Version2Deserializer
Version3Deserializer
....
VersionNDeserializer
Serializer

When ReadXml is called on the Person Class, the NamespaceURI property is
checked.  Based on the value of that property a Version[1 2 or 3]Deserializer
is created via the DeSerializationFactory.  The resultant serializer
deserializes the state and then life is good.

When WriteXml is called, the default Serializer instance is created and the
state is written as XML (always the latest version).

This seems to be working for me, but I was curious how other folks mitigate
this.

AddThis Social Bookmark Button