Serialization:
Serialization is the process of convert of .Net object into
another format like xml or Binary format.
This process used when we need save the object data as a
file or transfer across through the network, so this type conversion is called
as serialization.
The different types of serialization are
· Binary Serialization
· XML Serialization
· SOAP Serialization
Binary Serialization
Binary serialization is the process where you convert your .NET objects into byte stream. In binary serialization all the public, private, even those which are read only, members are serialized and converted into bytes. So when you want a complete conversion of your objects to bytes then you can make use of binary serialization.
When to use Binary Serialization
Object contains secure data then you can go with binary serialization, because it’s not easily human readable its completely convert the whole format.
Its file size is very small
If the two deserialize system has same also you can use Binary Serialization.
XML Serialization
It’s the process .Net object state is transformed into xml format in xml serialization.so the whole object has stored as xml file.
In XML serialization only the public properties and fields of the objects are converted into XML. The private members are not taken into consideration in XML serialization.
When to use XML Serialization
If you have an application in different platform then you can use XML Serialization.
If you are going to use only public members then you can use XML Serialization.
SOAP Serialization
It likes XML serialization. When you serialize object to SOAP format it conforms to the SOAP specification.
Example for XML Serialization:
Serialize the Object:
Initially I create one class for doing serialize
[XmlRoot("serialName")]
public class ser
{
[XmlElement("Name1")]
public String
str1;
[XmlElement("Name2")]
public String
str2;
[XmlElement("Name3")]
public String
str3;
}
|
XML Serialization Code:
Here I create new object for class and convert into xml
format and save the file as new.xml
ser s = new ser();
s.str1 = "name11";
s.str2 = "name22";
s.str3 = "name33";
FileStream str = new FileStream(Server.MapPath("new.xml"), FileMode.Create);
XmlSerializer xmls = new XmlSerializer(s.GetType());
xmls.Serialize(str, s);
str.Dispose();
|
XML Deserialization Code:
Here I Deserialze the convert xml object in to class
ser s = new ser();
StreamReader strread;
strread = new StreamReader(Server.MapPath("new.xml"));
XmlSerializer xmls = new XmlSerializer(s.GetType());
s = (ser)xmls.Deserialize(strread);
strread.Close();
|
Example for Binary Serialization:
Serialize the Object:
Initially I create one class for doing serialize
[Serializable]
public class ser
{
public String
str1;
public String
str2;
public String
str3;
}
|
Binary Serialization Code:
Here I create new object for class and convert into xml
format and save the file as new.xml
Instead of class you can convert string type also..
ser s = new ser();
s.str1 = "name1";
s.str2 = "name2";
s.str3 = "name3";
string stre = "vijay";
FileStream Fs = new FileStream(Server.MapPath("Bin.data"), FileMode.Create);
BinaryFormatter BF = new BinaryFormatter();
BF.Serialize(Fs, s);
Fs.Close();
|
Binary Deserialization Code:
Here I Deserialze the convert xml object in to class
ser s
= new ser();
FileStream Fs = new FileStream(Server.MapPath("Bin.data"), FileMode.Open);
BinaryFormatter BF = new BinaryFormatter();
s = (ser)BF.Deserialize(Fs);
Fs.Close();
|