In C#, how do I use an XmlSerializer
to deserialize an object that might be of a base class, or of any of several derived classes without knowing the type befor
If you're not set upon using the XmlSerializer
you can use the DataContractSerializer with the KnownType attribute instead.
All you need to do is add a KnownType
attribute to the parent class for each sub class and the DataContractSerializer
will do the rest.
The DataContractSerializer
will add type information when serializing to xml and use that type information when deserializing to create the correct type.
For example the following code:
[KnownType( typeof( C2 ) )]
[KnownType( typeof( C3 ) )]
public class C1 {public string P1 {get;set;}}
public class C2 :C1 {public string P2 {get;set;}}
public class C3 :C1 {public string P3 {get;set;}}
class Program
{
static void Main(string[] args)
{
var c1 = new C1{ P1="c1"};
var c2 = new C2{ P1="c1", P2="c2"};
var c3 = new C3{ P1="c1", P3="c3"};
var s = new DataContractSerializer( typeof( C1 ) );
Test( c1, s );
Test( c2, s );
Test( c3, s );
}
static void Test( C1 objectToSerialize, DataContractSerializer serializer )
{
using ( var stream = new MemoryStream() )
{
serializer.WriteObject( stream, objectToSerialize );
stream.WriteTo( Console.OpenStandardOutput() );
stream.Position = 0;
var deserialized = serializer.ReadObject( stream );
Console.WriteLine( Environment.NewLine + "Deserialized Type: " + deserialized.GetType().FullName );
}
}
}
Will output :
<C1 xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<P1>c1</P1></C1>
Deserialized Type: ConsoleApplication1.C1
<C1 i:type="C2" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<P1>c1</P1><P2>c2</P2></C1>
Deserialized Type: ConsoleApplication1.C2
<C1 i:type="C3" xmlns="http://schemas.datacontract.org/2004/07/ConsoleApplication1" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
<P1>c1</P1><P3>c3</P3></C1>
Deserialized Type: ConsoleApplication1.C3
In the output you'll notice the xml for c2 and c3 contained extra type information which allowed the DataContractSerializer.ReadObject
to create the correct type.
I recently wrote this generic serializer\deserializer for base class T and any derived classes of T. Seems to work so far.
The Type[] array stores all the derived types of T and T itself. The deserializer tries each of them, and returns when it found the right one.
/// <summary>
/// A generic serializer\deserializer
/// </summary>
/// <typeparam name="T"></typeparam>
public static class Serializer<T>
{
/// <summary>
/// serialize an instance to xml
/// </summary>
/// <param name="instance"> instance to serialize </param>
/// <returns> instance as xml string </returns>
public static string Serialize(T instance)
{
StringBuilder sb = new StringBuilder();
XmlWriterSettings settings = new XmlWriterSettings();
using (XmlWriter writer = XmlWriter.Create(sb, settings))
{
XmlSerializer serializer = new XmlSerializer(instance.GetType());
serializer.Serialize(writer, instance);
}
return sb.ToString();
}
/// <summary>
/// deserialize an xml into an instance
/// </summary>
/// <param name="xml"> xml string </param>
/// <returns> instance </returns>
public static T Deserialize(string xml)
{
using (XmlReader reader = XmlReader.Create(new StringReader(xml)))
{
foreach (Type t in types)
{
XmlSerializer serializer = new XmlSerializer(t);
if (serializer.CanDeserialize(reader))
return (T)serializer.Deserialize(reader);
}
}
return default(T);
}
/// <summary>
/// store all derived types of T:
/// is used in deserialization
/// </summary>
private static Type[] types = AppDomain.CurrentDomain.GetAssemblies()
.SelectMany(s => s.GetTypes())
.Where(t => typeof(T).IsAssignableFrom(t)
&& t.IsClass
&& !t.IsGenericType)
.ToArray();
}
Have you some root class/tag which contains that derived types? If yes, you can use XmlElementAttribute to map tag name to type:
public class RootElementClass
{
[XmlElement(ElementName = "Derived1", Type = typeof(Derived1BaseType))]
[XmlElement(ElementName = "Derived2", Type = typeof(Derived2BaseType))]
[XmlElement(ElementName = "Derived3", Type = typeof(Derived3BaseType))]
public BaseType MyProperty { get; set; }
}
public class BaseType { }
public class Derived1BaseType : BaseType { }
public class Derived2BaseType : BaseType { }
public class Derived3BaseType : BaseType { }
You could try to use the constructor XmlSerializer(Type type, Type[] extraTypes) to create a serializer that works with all involved types.
you can use XmlInclude
[XmlInclude(typeof(MyClass))]
public abstract class MyBaseClass
{
//...
}
otherwise if you want to add the types when serializing:
Type[] types = new Type[]{ typeof(MyClass) }
XmlSerializer serializer = new XmlSerializer(typeof(MyBaseClass), types);