Serialize object graph to XML in .Net

How to serialize any data structure to XML? My first idea was XmlSerializer. But then I found out it had some serious drawbacks. Luckily, there is a better option - NetDataContractSerializer.

In .Net, there are a few classes for (de)serialization. This is an overview of their features:


XmlSerializer

  • Cannot serialize circular references.
  • If more objects point to the same object, its copies are created in the xml for each of these references.
  • Has to know all types that could be encountered during serialization in advance - throws an exception on unknown type. Known types are passed in the constructor to XmlSerializer or marked by XmlIncludeAttribute.
  • Generates simple readable xml.

DataContractSerializer
  • Has to know types in advance - like XmlSerializer.
  • Can serialize circular references - construct with preserveObjectReferences = true
  • Used by WCF.
NetDataContractSerializer - better!
  • Serializes object graph properly including circular references.
  • Doesn't have to know types in advance.
  • However, MSDN states that it can be only used in .Net <-> .Net communication, which is ok also for storing object in a file.
  • Generates simple readable xml.
BinarryFormatter
  • Works well, like NetDataContractSerializer.
  • Disadvantage is that it serializes to binary format, which make its unusable e.g. for saving to a file that user could later edit.
  • The output is smallest thanks to the binary format.
SoapFormatter
  • Deprecated. Cannot serialize generic collections
- All serializers need the type to be serialized marked by SerializableAttribute.

kick it on DotNetKicks.com

What does it mean that XmlSerializer has to know all types that could be encountered during serialization in advance?
Imagine that we have two classes: Base and Derived.

[Serializable]

public class Base

{

public string name;

public Base()

{

name = "base instance";

}

}

[Serializable]

public class Derived : Base

{

public Derived left;

public Derived right;

public Derived()

{

}

}


What if we have a reference to Base and we actually don't want to care about the actual type?

Base b = new Derived();

// we only know we are holding reference to Base

// and we don't want to care about the actual type

XmlSerializer ser = new XmlSerializer(typeof(Base));

// serialize

using (FileStream fs = File.Create(AppDomain.CurrentDomain.BaseDirectory + "data.xml"))

{

// XmlSerializer throws an Exception

ser.Serialize(fs, b);

}

// deserialize

using (FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "data.xml"))

{

Base baseDeserialized = (Base)ser.Deserialize(fs);

Derived deserialize = baseDeserialized as Derived;

}


XmlSerializer throws an exception, because it encounters an "unknown" type - Derived. We could solve this by passing all the possible derived types in constructor of XmlSerializer or tagging all by XmlIncludeAttribute. This is of course inconvenient if you have a lot of classes. The worst thing is that when you add a derived class, you have to change code elsewhere.
NetDataContractSerializer doesn't have this problem.

The second issue with XmlSerializer is that it cannot serialize complex object graph. What does it mean "to serialize object graph"?

Derived top = new Derived();

top.left = new Derived();

top.left.name = "left son";

top.right = new Derived();

top.right.name = "right son";

top.left.right = new Derived();

// top

// / \

// left right

// \ /

// bottom

top.right.left = top.left.right;

XmlSerializer ser = new XmlSerializer(typeof(Derived));

using (FileStream fs = File.Create(AppDomain.CurrentDomain.BaseDirectory + "data.xml"))

{

ser.Serialize(fs, top);

}

using (FileStream fs = File.OpenRead(AppDomain.CurrentDomain.BaseDirectory + "data.xml"))

{

Derived deserialized = (Derived)ser.Deserialize(fs);

// false - we want true

bool ok = deserialized.left.right == deserialized.right.left;

}


After deserialization,
deserialized.left.right == deserialized.right.left is false, that means the object graph is different. Worse - XmlSerializer cannot serialize circular references at all.
Again, NetDataContractSerializer doesn't have any of these problems.

kick it on DotNetKicks.com

Posted by Martin Konicek on 10:59 PM

4 comments:

thoward37 said...

Just a short note: This class is only available in Framework versions 3.0 or later. Anyone writing for 2.0 compatability won't be able to take advantage of NetDataContractSerializer.

Inv said...

That is right of course. Thank you!

Sly said...

"need the type to be serialized marked by SerializableAttribute" - Only BinaryFormatter and SoapFormatter have this restriction. (Note that these serializers only serialize private data, and you have no control over the format). These two formatters also include .NET type information.

XmlSerializer does not need any attributes at all by default. (Note it only serializes public data.) It does not include .NET information (so you need to provide types).

Also, it is possible to initialize XmlSerializer with SoapReflectionImporter, which does allow complex object graphs (even circular references); this has been available since .NET 1.0, so is an option if you don't have the newer frameworks.

There is also an optional IXmlSerializable interface that works with XmlSerializer for precise custom control over formatting.

DataContractSerializer and NetDataContractSerializer try to be inclusive of the older formats. The have their own DataContractAttribute which allows either public or private data to be serialized (with the DataMemberAttribute).

DCS and NDCS also recognize the older SerializableAttribute (so classes can port across) and the latest version (3.5) also automatically works even with no attributes (like XmlSerializer).

DCS and NDCS both are closely related and meant to replace the older serialization (DCS for XmlSerializer and NDCS, with .NET type information, for BinaryFormatter/SoapFormatter).

In some cases you will still see XmlSerializer used as it is more flexible than DCS. (WCF defaults to DCS but will automatically switch to XmlSerializer if necessary).

dot net training said...

Your article was really informative and conveys whole concept in detail. Thanks for sharing..

Post a Comment