You must Sign In to post a response.

What is Serialization? SoapFormatter, BinaryFormatter, XmlFormatter?

What is Serialization? SoapFormatter, BinaryFormatter, XmlFormatter?


Author: ChandraShekar Thota23 Jul 2008 Member Level: Gold   Points : 4

Serialization - The process of converting an object into a stream of bytes. This stream of bytes can be persisted. Deserialization is an opposite process, which involves converting a stream of bytes into an object. Serialization is used usually during remoting (while transporting objects) and to persist file objecst & database objects.

Author: Ultimaterengan23 Jul 2008 Member Level: Gold   Points : 1

in the context of data storage and transmission, serialization is the process of saving an object onto a storage medium (such as a file, or a memory buffer) or to transmit it across a network connection link in binary form. When the resulting series of bytes is reread according to the serialization format, it can be used to create an accurate clone of the original object.

This process of serializing an object is also called deflating or marshalling an object. The opposite operation, extracting a data structure from a series of bytes, is deserialization (which is also called inflating or unmarshalling).

Author: chandramohan24 Jul 2008 Member Level: Gold   Points : 1

Serialization - The process of converting an object into a stream of bytes. This stream of bytes can be persisted. Deserialization is an opposite process, which involves converting a stream of bytes into an object. Serialization is used usually during remoting (while transporting objects) and to persist file objecst & database objects

Serialization - The process of converting an object into a stream of bytes. This stream of bytes can be persisted. Deserialization is an opposite process, which involves converting a stream of bytes into an object. Serialization is used usually during remoting (while transporting objects) and to persist file objecst & database objects.

.NET provides 2 ways for serializtion 1) XmlSerializer and 2) BinaryFormatter/SoapFormatter

The XmlSerializer is used for Web Services. The BinaryFormatter & SoapFormatter is used for Remoting. While using XmlSerializer, it is required that the target class has parameter less constructors, has public read-write properties and has fields that can be serialized. The XmlSerializer has good support for XML documents. It can be used to construct objects from existing XML documents. The XmlSerializer enables us to serialize and deserialize objects to an XML format.

SoapFormatter enables us to serialize & deserialize objects to SOAP format. They can serialize private and public fields of a class. The target class must be marked with the Serializable attribute. On deserialization, the constructor of the new object is not invoked.

The BinaryFormatter has the same features as the SoapFormatter except that it formats data into binary format. The BinaryForamatter (and the SoapFormatter) has two main methods. Serialize and Deserialize. To serialize an object, we pass an instance of the stream and the object to the Serialize method. To Deserialize an object, you pass an instance of a stream to the Deserialize method.

You can use the BinaryFormatter to serialize many, but not all, classes in the .NET Framework. For example, you can serialize ArrayLists, DataSets, and Arrays but not other objects, such as DataReaders or TextBox controls. To serialize a class, the class must have the Serializable attribute or implement the ISerializable interface.

Note that the XmlSerializer captures only the public members of the class, whereas the BinaryFormatter & the SoapFormatter captures both the public & private members of the class. The output using the BinaryFormatter is quite compact, as the information is in binary format, whereas the XmlSerializer format is filled with XML tags. See example below...

Imports System.IOImports System.Runtime.Serialization.Formatters.Binary
Dim colArrayList As ArrayListDim
objFileStream As FileStreamDim
objBinaryFormatter As BinaryFormattercolArrayList = New ArrayList()
colArrayList.Add( "Whisky")
colArrayList.Add( "Vodka")
colArrayList.Add( "Brandy")
objFileStream = New FileStream(MapPath("C:\"), FileMode.Create)
objBinaryFormatter = New BinaryFormatterobjBinaryFormatter.Serialize(objFileStream, colArrayList)objFileStream.Close()

Here we see that an instance of the file stream (objFileStream) and an instance of the object (colArrayList) is passed to the Serialize method of the BinaryFormatter object (objBinaryFormatter). We also end up creating a file by the name on our hard-drive.In order to deserialize an object, see the code below…

Dim colArrayList As ArrayListDim objFileStream As FileStreamDim
objBinaryFormatter As BinaryFormatterDim strItem As String
objFileStream = New FileStream( MapPath(""), FileMode.Open )
objBinaryFormatter = New BinaryFormatter
colArrayList = CType( objBinaryFormatter.Deserialize( objFileStream ), ArrayList )
For Each strItem In colArrayList
Response.Write( " " & strItem )

Here, CType takes in two parameters, the first parameter is the serialized object in the file stream format, and the second parameter is the desired type. Finally, the page iterates through all the elements of the ArrayList and displays the value of each element.

XmlSerializer does not serialize instances of classes like Hashtable which implement the IDictionary interface.

Serializable - This is a class attribute. When we use this attribute with a class, an instance of this class can be taken in whatever state it is, and write it to a disk. The class can then be deserialized, and the class will act as if it is simply stored in the memory.

The code accompanied by this article contains our XmlFormatter class that has the following features:

Produces more clean XML than the SOAP structure of the SoapFormatter
Supports the ISerializable interface
Does not impact performance by creating in-memory assemblies
The following example shows the use of the formatter, by serializing a class called MyObject:

using System;
using System.Collections.Generic;
using System.Text;
using System.Runtime.Serialization;
namespace XmlFormatterSample
public class MyObject : ISerializable
public string MyPublicProperty
get { return _myPublicProperty; }
set { _myPublicProperty = value; }
private string _myPublicProperty;
private string MyPrivateProperty
get { return _myPrivateProperty; }
set { _myPrivateProperty = value; }
private string _myPrivateProperty;
public MyObject()
this._myPublicProperty = "This is my public property";
this._myPrivateProperty = "This is my private property";
public string GetProperties()
string properties = "MyPublicProperty = " +
MyPublicProperty + "\r\n";
properties += "MyPrivateProperty = " +
return properties;
#region ISerializable Members

/// Special serialization constructor.

public MyObject(SerializationInfo info,
StreamingContext context)
_myPublicProperty = info.GetString("MyPublicProperty");
_myPrivateProperty = info.GetString("MyPrivateProperty");
/// Interface method to place the properties
/// in the serialization queue

public void GetObjectData(SerializationInfo info,
StreamingContext context)
info.AddValue("MyPublicProperty", MyPublicProperty);
info.AddValue("MyPrivateProperty", MyPrivateProperty);
The following code serializes and deserializes the object from and to a MemoryStream using our XmlFormatter. Please note that both the public and the private properties are being serialized.

using DotNetMagazine.September.Examples;
using System.IO;
using System.Xml;
using System.Data.SqlTypes;
namespace XmlFormatterSample
class Program
static void Main(string[] args)
MyObject object1 = new MyObject();
MyObject object2;
// write the properties to the console
Console.WriteLine("The properties of object1 are:");
// serialize the object
// ***************************************
MemoryStream stream = new MemoryStream();
XmlFormatter serializer =
new XmlFormatter(typeof(MyObject));
serializer.Serialize(stream, object1);
// reset the stream to the beginning
stream.Position = 0;
SqlXml xml = new SqlXml(stream);
// ***************************************
// write the XML value to the console
Console.WriteLine("Xml value of object 1:");

// recreate the object in object 2
using (MemoryStream stream2 =
new MemoryStream(Encoding.UTF8.GetBytes(xml.Value)))
object2 = (MyObject)serializer.Deserialize(stream2);
// write the properties to the console
Console.WriteLine("The properties of object2 are:");
Console.WriteLine("Press any key to continue");
The code is free to use in your projects. This code is also part of an article published in the Dutch version of the .NET magazine, September issue 2006: "Objecten en Sql Server

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Serialization is a process of taking an object and converting into a form so that it can be transported across the network or can be persisted in the storage location. This storage location can be physical file, database or ASP.NET Cache. The form contains the state of the object so that by this format, we can construct the same object a later point in time, which is called Deserialization.

There are three formats of serialization

Binary Serialization : Light and compact used in Remoting

SOAP Serialization : interoperable use SOAP and used in web Services

XML Serialization : Custom Serialization

XML Serialization

For XML serialization, you need to use the attributes and specify them for each and every public member that you need. But since it is limited that it can serialize only public members, Serization done by it is called custom serialization. It is also known as Shallow Serialization

SOAP and Binary Serialization

XML serializes only public members of the class. You use SOAP or Binary serialization when you need to transport data across the network. SOAP sends it using HTTP Protocol which makes it most interoperable while Binary serialization is known for its light and compact nature. Web Services uses the SOAP Serialization and Remoting uses the Binary Serialization. Infact Serialization is always neccessary when you need the object to transfer across a network. Advantage of using the SOAP or Binary serialization is that you can serialize the entire object and all those object that are being refrenced by it. This is why it is also called Deep Serialization. If you want any class to serialize through any of these methods then you should use [Serializable] attribute on that class and then you can use the SoapFormater class or BinaryFormatter class to do the serialization. These classes have Serialize and DeSerialize method. If you will not use SerializableAttribute for the class, then it will raise the exception.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Serialization is the concept whereby an object is written into a linear stream. The .NET Framework provides an excellant support to serializing and deserializing objects.

Serialization is the process of saving the state of an object in a persistent storage media by converting the object to a linear stream of bytes. The object can be persisted to a file, a database or even in the memory. The reverse process of serialization is known as de-serialization and enables us to re-construct the object from the previously serialized instance of the same in the persistent or non-persistent storage media.

Serialization in .NET is provided by the System.Runtime.Serialization namespace. This namespace contains an interface called IFormatter which in turn contains the methods Serialize and De-serialize that can be used to save and load data to and from a stream. In order to implement serialization in .NET, we basically require a stream and a formatter. While the stream acts as a container for the serialized object(s), the formatter is used to serialize these objects onto the stream.

The basic advantage of serialization is the ability of an object to be serialized into a persistent or a non-persistent storage media and then reconstructing the same object if required at a later point of time by de-serializing the object.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Serialization is a process of converting an object into a stream of data so that it can be is easily transmittable over the network or can be continued in a persistent storage location. This storage location can be a physical file, database or ASP.NET Cache. Serialization is the technology that enables an object to be converted into a linear stream of data that can be easily passed across process boundaries and machines. This stream of data needs to be in a format that can be understood by both ends of a communication channel so that the object can be serialized and reconstructed easily. The advantage of serialization is the ability to transmit data across the network in a cross-platform-compatible format, as well as saving it in a persistent or non-persistent storage medium in a non-proprietary format. Serialization is used by Remoting, Web Services SOAP for transmitting data between a server and a client. De-serialization is the reverse; it is the process of reconstructing the same object later. The Remoting technology of .NET makes use of serialization to pass objects by value from one application domain to another.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Advantages and Disadvantages of Serialization

The following are the basic advantages of serialization:

· Facilitate the transportation of an object through a network

· Create a clone of an object

The primary disadvantage of serialization can be attributed to the resource overhead (both the CPU and the IO devices) that is involved in serializing and de-serializing the data and the latency issues that are involved for transmitting the data over the network. Further, serialization is quite slow. Moreover, XML serialization is insecure, consumes a lot of space on the disk and it works on public members and public classes and not on the private or internal classes. Therefore, it compels the developer to allow the class to be accessed to the outside world.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Types of Serialization

Serialization can be of the following types:

· Binary Serialization

· SOAP Serialization

· XML Serialization

· Custom Serialization

Binary Serialization

Binary serialization is a mechanism which writes the data to the output stream such that it can be used to re-construct the object automatically. The term binary in its name implies that the necessary information that is required to create an exact binary copy of the object is saved onto the storage media. A notable difference between Binary serialization and XML serialization is that Binary serialization preserves instance identity while XML serialization does not. In other words, in Binary serialization the entire object state is saved while in XML serialization only some of the object data is saved. Binary serialization can handle graphs with multiple references to the same object; XML serialization will turn each reference into a reference to a unique object.

SOAP Serialization
The SOAP protocol is ideal for communicating between applications that use heterogeneous architectures. In order to use SOAP serialization in .NET we have to add a reference to System.Runtime.Serialization.Formatters.Soap in the application. The basic advantage of SOAP serialization is portability. The SoapFormatter serializes objects into SOAP messages or parses SOAP messages and extracts serialized objects from the message.

XML Serialization
According to MSDN, "XML serialization converts (serializes) the public fields and properties of an object or the parameters and returns values of methods, into an XML stream that conforms to a specific XML Schema definition language (XSD) document. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport. Because XML is an open standard, the XML stream can be processed by any application, as needed, regardless of platform." Implementing XML Serialization in .Net is quite simple. The basic class that we need to use is the XmlSerializer for both serialization and de-serialization. The Web Services use the SOAP protocol for communication and the return types and the parameters are all serialized using the XmlSerializer class. XML Serialization is however, much slower compared to Binary serialization.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Working with Formatters

A formatter is used to determine the serialization format for objects. In other words, it is used to control the serialization of an object to and from a stream. They are the objects that are used to encode and serialize data into an appropriate format before they are transmitted over the network. They expose an interface called the IFormatter interface. IFormatter's significant methods are Serialize and De-serialize which perform the actual serialization and de-serialization. There are two formatter classes provided within .NET, the BinaryFormatter and the SoapFormatter. Both these classes extend the IFormatter interface.

The Binary Formatter

The Binary formatter provides support for serialization using binary encoding. The BinaryFormater class is responsible for binary serialization and is used commonly in .NET's Remoting technology. This class is not appropriate when the data is supposed to be transmitted through a firewall.

The SOAP Formatter

The SOAP formatter provides formatting that can be used to serialize objects using the SOAP protocol. It is used to create a Soap envelop and it uses an object graph to generate the result. It is responsible for serializing objects into SOAP messages or parsing the SOAP messages and extracting these serialized objects from the SOAP messages. SOAP formatters in .NET are widely used by the Web Services.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Serialization in the .NET Framework

Serialization in .NET allows the programmer to take an instance of an object and convert it into a format that is easily transmittable over the network, or even stored in a database or file system. This object will actually be an instance of a custom type, including any properties or fields you may have set.

Let's take a scheduling object as an example. The scheduling object, enables an application to trigger events based on a very specific schedule. There could be numerous settings involved when building this, such as intervals, date and time restrictions, etc. Normally each piece of data would require the creation of a separate field in your table, when really the only important piece of information is the exact date/time when the next event should be triggered. Instead, serialize this entire scheduling object, and save that into the database, along with the one piece of information that is needed, the next event date/time. This cuts down the entire database schema to about three fields, not to mention the time saved from writing complex Save and Load methods.

Author: Ashok Khanna24 Jul 2008 Member Level: Gold   Points : 1

Definition of Serialization

Serialization is the process of taking an object and converting it to a format in which it can be transported across a network or persisted to a storage location. The storage location could be as simple as using a file or a database. The serialized format contains the object's state information. Deserialization is the process of using the serialized state information to reconstruct the object from the serialized state to its original state. In essence, the process of serialization allows an object to be serialized, shipped across the network for remoting or persisted in a storage location such as the ASP.NET cache, and then be reconstructed for use at a later point in time.
Serialization Formats

There are three formats provided by the Microsoft .NET framework to which objects can be serialized. The formats are binary, SOAP, and XML. The format is controlled based upon what object is used to perform the serialization. The XML format is produced by using the System.Xml.Serialization.XmlSerializer class. The SOAP and binary formats are produced by using classes under the System.Runtime.Serialization.Formatters namespace.

There are subtle differences among the serialized formats. The binary-based format is the most compact and light of the three formats. The XML formatter only serializes public fields and properties, while binary and SOAP do not adhere to that limitation.
Controlling XML Serialization with Attributes

Attributes can be used to control the serialization of an object performed by the XmlSerializer. Some of the attributes available are as follows:

* XmlAttributeAttribute—The member will be serialized as an XML attribute
* XmlElementAttribute—The field or property will be serialized as an XML element
* XmlIgnoreAttribute—The field or property will be ignored when serializing
* XmlRootAttribute—Represents the XML document's root element name, only applies to a class

Author: Ratheesh31 Jul 2008 Member Level: Gold   Points : 1

Serialization is used for object persitence. using this you
can store your object current state in disk. The same state
you can get after restarting your application.

A serializable class usually has a Serialize member
function, and it usually uses the DECLARE_SERIAL and
IMPLEMENT_SERIAL macros, as described under class CObject.

The overloaded extraction (>>) and insertion (<<) operators
are convenient archive programming interfaces that support
both primitive types and CObject-derived classes.

Serialization is a process used to read and write object
from and to secondary storage device respectively.Base
class is CArchive class of MFC. it is used basically in
doc-view architecture programming.DECLARE_SERIAL and
IMPLEMENT_SERIAL macro is used in header and .cpp file
respectively, which has to be serialize.

Sign In to post your comments