0

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.

Shimmy Weitzhandler
  • 101,809
  • 122
  • 424
  • 632

1 Answers1

0

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>  
John Saunders
  • 160,644
  • 26
  • 247
  • 397
John Wigger
  • 904
  • 8
  • 10
  • I wouldn't call it an Easter egg. It's used in some of the .NET-specific bindings by default. – John Saunders Feb 03 '11 at 18:58
  • @John Saunders, out of curiosity, which bindings are you referring to? I assume you mean WCF bindings. I didn't realize that it was used anywhere. I thought that the DataContractSerializer was the default. NetDataContractSerializer is rarely mentioned anywhere (one page in Juval Lowy's Programming WCF Services 3rd edition). – John Wigger Feb 03 '11 at 20:25