Dynamic XmlSerializer that identifies the object type from the xml-serialization?

767 views Asked by At

Is there any way to create an XmlSerializer that stores along with the serialized data the data type, then when deserializing, it automatically identifies the type of the serialized object and creates an object of that type (returned as object).

Any idea that includes XDocument, XML-literals would be welcommed too.

1

There are 1 answers

2
John Wigger On BEST ANSWER

Regarding John Saunders' good observation about the NetDataContractSerializer. The NetDataContractSerializer is pretty obscure, almost an Easter Egg for XML Serialization. I have VB.NET specific blog post that shows how to create the source code for the necessary attribute, and what the resultant XML looks, http://bejabbers2.blogspot.com/2010/03/making-case-for-netdatacontractserializ.html. Here is what a serialized class looks like

<s:Body>   
<DetermineCoordinates xmlns="http://tempuri.org/">   
<LocationInfo z:Id="1" z:Type="SharedTypes.LocationInfo" z:Assembly="SharedTypes, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null" xmlns="http://schemas.datacontract.org/2004/07/SharedTypes" xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns:z="http://schemas.microsoft.com/2003/10/Serialization/">
   <latitude>0</latitude>   
<longitude>0</longitude>   
<postalCode z:Id="2">90125</postalCode>   
</LocationInfo>   
</DetermineCoordinates>   
</s:Body>