Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

I'm confused - when should I be using XML Serialization and when should I be using Binary Serialization in the .NET framework?

like image 332
Shashi Avatar asked Jan 20 '11 04:01

Shashi


People also ask

When should we use binary serialization as compared to XML serialization?

Xml Serializer serializes only public member of object but Binary Serializer serializes all member whether public or private. In Xml Serialization, some of object state is only saved but in Binary Serialization, entire object state is saved.

What are the different types of serialization supported in .NET framework?

There are three types of serialization in . Net : Binary Serialization, SOAP Serialization and XML Serialization. Binary serialization is the process where you convert your . NET objects into byte stream.

Why do we serialize XML?

Advantages of Using XML Serialization For example, XmlSerializer enables you to: Specify whether a field or property should be encoded as an attribute or an element. Specify an XML namespace to use. Specify the name of an element or attribute if a field or property name is inappropriate.

What is the limitation of XML serialization?

XML serialization has some limitations. It converts public property values and fields, but does not encode type information. Also it does not encode private properties and fields, which requires binary serialization. Any class to be serialized to XML must have a public, parameterless constructor.


3 Answers

Both of the existing answers focus on "cross platform", but that is an unrelated issue. The point they are making there is "don't use BinaryFormatter if you are doing cross-platform" - which I entirely support. However there are a range of binary serialization formats that are very much cross-platform - protobuf / ASN.1 being prime examples.

So, let's look instead at what each has to offer;

  • Binary is typically smaller, typically faster to process (at both ends), and not easily human readable / editable
  • Text formats (xml / json) tend to be more verbose than binary (although often compresses well), but are pretty easy to work with by hand; but all that text processing an mapping tends to make them slower
    • xml is very common is web-services, and benefits from gooling support such as xsd, xslt and robust xml editors
    • json is the main player in browser-based comms (although it is also used in web-services) - tends to be less formal but still very effective

Notice how interoperability is neither a strength nor weakness of either, as long as you choose an appropriate binary format!

Here's an answer that compares the serialization time, deserialization and space metrics of most of the .NET serializers, for your reference.

like image 87
Marc Gravell Avatar answered Oct 19 '22 19:10

Marc Gravell


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.

like image 34
Adeel Avatar answered Oct 19 '22 21:10

Adeel


If you want user friendly or cross platform output, then use XML. Also, XML serialization use public fields and properties to serialize and you can change/reformat output by using attributes and custom serialization on class level if you whant. Bin.Ser. uses private fields to serialize.

like image 2
SeeSharp Avatar answered Oct 19 '22 20:10

SeeSharp