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. In this article I will discuss .NET's support for Serialization and how we can build a class that supports custom serialization.
If you are using Custom Objects to store data, you can maintain state of the object across postbacks by storing the object in the ViewState.
To do this your object should be Serializable.
Also, at times, if you want to store data temporarily, it is easier to serialize your object into xml and store it rather than storing it in db.
Following namespaces are involved in serialization process :
Here we have our own class named ClassToSerialize. This class has two public valiables name and age with some default values. We will write this class to a disk file (temp.dat) using SerializeTest class.
SerializeTest class has two methods SerializeNow() and DeSerializeNow() which perform the task of serialization and deserialization respectively.
The general steps for serializing are :
Create an instance of File that will store serialized object.
Create a stream from the file object.
Create an instance of BinaryFormatter.
Call serialize method of the instance passing it stream and object to serialize.
The steps for de-serializing the object are similar. The only change is that you need to call deserialize method of BinaryFormatter object.
Now, let us see an example where we have used 'real' class with public and shared members and properties to encapsulate them. The class also uses another supporting class. This is just to make clear that if your class contains further classes, all the classes in the chain will be serialized.
Serialize .NET objects to store and retrieve them from a file or viewstate in ASP.NET
Everything is .NET is an object. But sometimes you need to store an object to a file, ASP.NET's viewstate etc. then get it back from the file or viewstate and continue to use it as the object it originally was.
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.