码迷,mamicode.com
首页 > Web开发 > 详细

Serialize object graph to XML in .Net

时间:2015-08-20 20:36:25      阅读:224      评论:0      收藏:0      [点我收藏+]

标签:

 

http://coding-time.blogspot.hk/2008/03/serialize-object-graph-to-xml-in-net.html

 

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.

技术分享

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.

Serialize object graph to XML in .Net

标签:

原文地址:http://www.cnblogs.com/lilei9110/p/4746017.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!