Join the social network of Tech Nerds, increase skill rank, get work, manage projects...
 
  • Binary serialization vs XML serialization in .net

    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 0
    • 926
    Comment on it

    There are many differences between  Binary serialization and XML serialization. Some of them are given below:

     

    Binary serialization XML serialization 
    1. In Binary serialization, all members are encoded in binary form.  1. In XML serialization, all members are encoded in XML form that makes the code more readable.
    2. It provides better support as compared to XML serialization when storing data.  2. It provides less support than Binary serialization when storing data.
    3. Binary serialization is used when we are using same application platform. 3. XML serialization is used if we are using different application platform.
    4. It serialize all the fields of an object unless specified NonSerializable     4. It serialize only the public fields and properties of an object.
    5.It serialize private members of a class which is called as deep serialization.   5. It do not serialize the private members of a class which is called as shallow serialization.
    6.There is no easy control of each serialized member.   6. There is easy control of each serialized member by using attributes.
    7. It saves metadata in output stream for deserialization. 7. It does not save metadata in output stream for deserialization.
    8. There is no need of public constructor and public access.  8. Class should have public constructor and public access.
    9.In binary serialization, there is no need to specify the type to be serialized or deserialized. 9. In XMl serialization, we have to specify the type of object of type to be serialized or deserialized.
    10.Binary serialization preserves instance identity which mean entire object state is saved.  10. XML serialization does not preserve instance identity because in XML serialization only some of the object data is saved.
    11. Binary serialization can handle graphs with multiple references to same object.   11. XML serialization turn each reference into unique object.
    12.In Binary serialization, we use namespace called System.RunTime.Serialization.Formatters.Binary 12. In XML serialization, we use namespace called  System.Xml.Serialization
    13. Binary serialization is more efficient and faster than XML serialization. It saves memory and bandwidth and easier to parse than XML. 13. XML serialization is less efficient and faster than Binary serialization.
    14. Binary Serializer need serializable attribute  on the class. 14. XML Serializer no need serializable attribute on the class.

     

    Example of Binary serialization:

    //Adding namespace:
    using System.Runtime.Serialization.Formatters.Binary;
    using System.Runtime.Serialization;
    
    public static void BinarySerialize(string filename, Object obj)
    {
    	FileStream fileStreamObject;
    	fileStreamObject = new FileStream(filename, FileMode.Create, FileAccess.Write);
    	BinaryFormatter binaryFormatter = new BinaryFormatter();
    	try
    	{
    		binaryFormatter.Serialize(fileStreamObject, obj);
    	}
    	catch (SerializationException ex)
    	{
    		Console.WriteLine("Failed to serialize : " + ex.Message);
    	}
    
    	fileStreamObject.Close();
    }

     

    Example of XML serialization

    //Adding namespace:
    
    using System.Runtime.Serialization;
    using System.Xml.Serialization;
    
    public static void XMLSerialize(String filename, Object obj)
    {
    	XmlSerializer serializer = null;
    	FileStream stream = null;
    	serializer = new XmlSerializer(typeof(Object));
    	stream = new FileStream(filename, FileMode.Create, FileAccess.Write);
    	try
    	{
    		serializer.Serialize(stream, obj);
    	}
    	catch (SerializationException ex)
    	{
    		Console.WriteLine("Failed to serialize : " + ex.Message);
    	}
    	if (stream != null)
    		stream.Close();
    }

     

 0 Comment(s)

Sign In
                           OR                           
                           OR                           
Register

Sign up using

                           OR                           
Forgot Password
Fill out the form below and instructions to reset your password will be emailed to you:
Reset Password
Fill out the form below and reset your password: