What Are the Differences Between the Xmlserializer and Binaryformatter

What are the differences between the XmlSerializer and BinaryFormatter

The reason a binary formatter is able to deserialize directly to an interface type is because when an object is originally serialized to a binary stream metadata containing type and assembly information is stuck in with the object data. This means that when the binary formatter deserializes the object it knows its type, builds the correct object and you can then cast that to an interface type that object implements.

The XML serializer on the otherhand just serializes to a schema and only serializes the public fields and values of the object and no type information other then that (e.g. interfaces the type implements).

Here is a good post, .NET Serialization, comparing the BinaryFormatter, SoapFormatter, and XmlSerializer. I recommend you look at the following table which in addition to the previously mentioned serializers includes the DataContractSerializer, NetDataContractSerializer and protobuf-net.

Serialization Comparison

Is there any perfomormance differences between binary and XML serialization?

Nope.

It depends highly on what sort of data is inside the XML document itself. If you have a lot of structured data, the overhead for XML will be large. For example, if your data looks like:

<person>
<name>Dave</dave>
<ssn>000-00-0000</ssn>
<email1>xxxxxx/email1>
</person>
...

You'll have a lot more overhead than if you have an XML document that looks like:

<book name="bible">
In the beginning God created the heavens and the earth.
Now the earth was formless and empty ...
And if any man shall take away from the words of the book of this prophecy, God shall take away his part out of the book of life, and out of the holy city, and from the things which are written in this book. He which testifieth these things saith, Surely I come quickly. Amen. Even so, come, Lord Jesus.
</book>

So it's not really a fair question. It depends highly on the data YOU intend to send, and how/if you're compressing it.

BinaryFormatter handles more cases than XmlSerializer?

No, it's not correct. Depends on many circumstances. In some cases, the BinaryFormatter is more convenient, in others the XmlSerializer.


XmlSerializer can serialize an array of mixed types. For this you need to use the XmlInclude attribute in the following way.

[XmlInclude(typeof(X))]
[XmlInclude(typeof(Y))]
public class ParentCls
{
public int k;
}

public class X : ParentCls
{
public string name = "foobar";
}

public class Y : ParentCls
{
public double price = 32.0;
}

Please note that you must use the public access modifier, that is a serious limitation of the XmlSerializer.

Example usage

ParentCls[] data = new ParentCls[] { new X(), new Y() };
var xs = new XmlSerializer(typeof(ParentCls[]));

using (Stream stream = File.Open("tmp.xml", FileMode.Create))
xs.Serialize(stream, data);

Using BinaryFormatter and XmlSerializer interchangeably

As has already been pointed out, not all objects that are binary serializable, are also XML serializable (for example, anything with TimeSpan).

But if you can deal with that pretty serious flaw, then the approach I would probably take is to create my own interface. I would then have 2 classes that implement it, one wrapping the binary formatter and one wrapping the XML serializer. To make life easier, have the interface very similar to the binary formatter in terms of method names your app uses and parameters so that you can replace occurrences of the concrete binary formatter relatively painlessly.

BinaryFormatter becomes slower than XmlSerializer the more Items I Serialize/Deserialize

Binary formatte searlizes all metadata like type, assembly information.

The XMLSerializer just serializes to a schema (public fields, values of object). so i think that's the reason why it's faster

http://blogs.msdn.com/b/youssefm/archive/2009/07/10/comparing-the-performance-of-net-serializers.aspx

When should I use XML Serialization vs. Binary Serialization in the .NET framework?

Specific to .NET, If you have two applications that are using the same type system, then you can use binary serialization. On the other hand if you have applications that are in different platforms then it is recommended to use XML Serialization. So if i am writing a chat application (client and server), I might use binary serialization, but if I later decide that I should use Python to write a client, then I may not.

Why is binary serialization faster than xml serialization?

Binary serialization is more efficient because write raw data directly and the XML needs format, and parse the data to generate a valid XML structure, additionally depending of what sort of data have your objects the XML may have a lot of redundant data.



Related Topics



Leave a reply



Submit