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

3

16

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

Smukler answered 20/1, 2011 at 4:31 Comment(1)
Just want? or any Should circumstance?Recompense
T
14

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.

Teresa answered 20/1, 2011 at 4:47 Comment(3)
If you include the Data Contract Serializer when you say "xml serialization", then I agree with you.Hybrid
@Mahantesh @Teresa - "binary" is not the issue here; It is not true that binary is not interoperable - simply: BinaryFormatter (a specific example of a binary serializer) is a poor choice. There are several others.Fractocumulus
@Adeel, do you prefer "than" than then?Recompense
F
37

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.

Fractocumulus answered 20/1, 2011 at 5:49 Comment(2)
Kinda curious, how do you find your old answers? Just today i tried to track down one of my recent answers to some related question and i had a hard time finding it.Decaliter
@H.B. usually via google, for exampleFractocumulus
T
14

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.

Teresa answered 20/1, 2011 at 4:47 Comment(3)
If you include the Data Contract Serializer when you say "xml serialization", then I agree with you.Hybrid
@Mahantesh @Teresa - "binary" is not the issue here; It is not true that binary is not interoperable - simply: BinaryFormatter (a specific example of a binary serializer) is a poor choice. There are several others.Fractocumulus
@Adeel, do you prefer "than" than then?Recompense
L
2

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.

Lais answered 20/1, 2011 at 5:10 Comment(3)
Sigh; oh the old myths... To contradict both points; while BinaryFormatter is indeed a field serializer, it is not the only binary serializer - most others are far better behaved - and likewise, DataContractSerializer writes xml, and unless the type is annotated correctly acts as a field serializer.Fractocumulus
DataContractSerializer, WCF? In this question no one word about that.Lais
my reference there is "xml serialization", which is not the same as XmlSerializer. "xml serialization" is simply a categorisation; XmlSerializer is a specific example of an API for "xml serialization". DataContractSerializer also performs "xml serialization".Fractocumulus

© 2022 - 2024 — McMap. All rights reserved.