Recently I did couple of projects where I needed to save objects in a file and then load back to the system for some processing. I thought of couple of solutions but at last stick to a simpler solution. In one of our project we did something like the bellow solutions.
Step 1: Serialize the related class in separate files.
Step 2: Use Open source zip technology to zip the files in a single file.
Step 3: Rename the zip file with custom extension for instance “filename.xte”
Serialize a collection of class objects
In this post I have some classes to show In case of a simple serialization context. Below I have my data class named “Bond” with only 5 properties. Below the code snippet is given how the class looks like.
Next I have a class which will be serialized and saved in a file. In this class I have a List of Bond class, and of course it implements the ISerializable interface. Below a code snippet is given to show the the code looks like.
The final piece of work that to the all kind of lifting is the serialize, below the code snippet to is given to demonstrate the idea. The responsibility of the below code is to serialize and de-serialize the objects.
In first method of above code we pass the file name and the object that need to serialize, it serialize the object with binary formatter and save in the file that is provided. In second method we provide a file name where the serialized file is it desterilize the file and build the wrapped object.
In above code I have a class named “BackUpManager” where we have two method, in first method using file open dialog and using Serializer class we saved the file. And in second method using Serializer class we saved a file, and in both case we have our own extension.
In the above short article we have seen a simple serialization scenario. And I explained why we need it. And also shown some classes to demonstrate the idea.