Serialization and De-serialization
Serialization - The process of converting an object into a stream of bytes. De-serialization is an
opposite process, which involves converting a stream of bytes into an object.
Serialization is useful when you want to save the state of your application to a persistence
storage area. That storage area can be a file. At a later time, you may restore these objects by
using the process of de-serialization.
Serialization in .NET
.NET provides two ways for serialization
1. XmlSerializer
2. BinaryFormatter/SoapFormatter
The XmlSerializer is used for Web Services. The BinaryFormatter & SoapFormatter is used for
Remoting.
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. 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.
SoapFormatter enables us to serialize & deserialize objects to SOAP format.
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 a class, the class must have the Serializable attribute or
implement the ISerializable interface.
Create a class library with Visual Studio that contains a class called employee. Add empNo,
empName, department and salary as public attributes to the employee class. Add the
[Serializable] attribute in front of the class' definition.
Note: Serializable 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.
Class libaray
of Employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace calssLib
{
[Serializable]
public class Employee
{
public string empNo;
public string
empName;
public string
department;
public double salary;
public Employee(string
eno,string empName,string
dept,double sal)
{
this.empNo = eno;
this.empName = empName;
this.department = dept;
this.salary = sal;
}
public Employee()
{
}
public string
getEmpNo()
{
return this.empNo;
}
public string
getName()
{
return this.empName;
}
public string
getDepartment()
{
return this.department;
}
public double
getSalary()
{
return this.salary;
}
}
}
Create two windows forms applications as given below
figure 01
Important
You must add reference to Class library(right click on solution explorer project file--> add reference-->Brows-->calssLib.DLL) that you created
You must add reference to soap-formatter (right click on solution explorer project file--> add reference--> .NET -->System.Runtime.Serialization.Formatters.Soap)
Serialization
XML,Binary,Soap
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using calssLib;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace Serializing
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//XML Button click event
private void
button1_Click(object sender, EventArgs e)
{
calssLib.Employee emp = new calssLib.Employee(textBox1.Text,
textBox2.Text, textBox3.Text, Convert.ToDouble(textBox4.Text));
FileStream
fs = new FileStream(@"C:\Users\Harsha\Documents\Visual Studio 2010\Projects\lab2\Serializing\xml",
FileMode.Create);
XmlSerializer xml = new
XmlSerializer(typeof(Employee));
xml.Serialize(fs, emp);
fs.Close();
MessageBox.Show("XML
file was created successfully");
}
//Binary Button click event
private void
button2_Click(object sender, EventArgs e)
{
calssLib.Employee emp = new calssLib.Employee(textBox1.Text,
textBox2.Text, textBox3.Text, Convert.ToDouble(textBox4.Text));
FileStream
fs = new FileStream(@"C:\Users\Harsha\Documents\Visual Studio
2010\Projects\lab2\Serializing\binary", FileMode.Create);
BinaryFormatter bin = new
BinaryFormatter();
bin.Serialize(fs, emp);
fs.Close();
MessageBox.Show("Binary file was created successfully");
}
//Soap Button click event
private void
button3_Click(object sender, EventArgs e)
{
calssLib.Employee emp = new calssLib.Employee(textBox1.Text,
textBox2.Text, textBox3.Text, Convert.ToDouble(textBox4.Text));
FileStream
fs = new FileStream(@"C:\Users\Harsha\Documents\Visual Studio
2010\Projects\lab2\Serializing\soap", FileMode.Create);
SoapFormatter xml = new
SoapFormatter();
xml.Serialize(fs, emp);
fs.Close();
MessageBox.Show("Soap file was created successfully");
}
}
}
Once data has been entered to the text fields in the form in figure 01 and ” Serialize” button is
pressed the application should create a object of the Employee class and serialize it to a file using
a given serialization method.
When the “Deserialize” button in the form in figure 02 is pressed the data in the file should be
de-serialized in to an employee object. Later the data in the object can be displayed in labels on
the form.
The BinaryForamatter ,SoapFormatter and the xmlSerializer has two main methods. Serialize
and Deserialize. To serialize an object, we pass an instance of the file stream and the object to
the Serialize method. To Deserialize an object, you pass an instance of a file stream to the
Deserialize method.
DeSerialization XML,Binary,Soap
figure 02
Important
You must add reference to Class library(right click on solution explorer project file--> add reference-->Brows-->calssLib.DLL) that you created
You must add reference to soap-formatter (right click on solution explorer project file--> add reference--> .NET -->System.Runtime.Serialization.Formatters.Soap)
DeSerialization
XML,Binary,Soap
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using calssLib;
using System.IO;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.Runtime.Serialization.Formatters.Soap;
namespace DeSeriallize
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
//XML Button click event
private void
button1_Click(object sender, EventArgs e)
{
Employee emp = new
Employee();
FileStream
fs = new FileStream(@"C:\Users\Harsha\Documents\Visual Studio
2010\Projects\lab2\Serializing\xml",
FileMode.Open, FileAccess.Read);
XmlSerializer xml = new
XmlSerializer(typeof(Employee));
emp = (Employee)xml.Deserialize(fs);
label5.Text = emp.getEmpNo();
label6.Text = emp.getName();
label7.Text = emp.getDepartment();
label8.Text=Convert.ToString(emp.getSalary());
}
//Binary Button click event
private void
button2_Click(object sender, EventArgs e)
{
Employee
emp = new Employee();
FileStream fs = new
FileStream(@"C:\Users\Harsha\Documents\Visual
Studio 2010\Projects\lab2\Serializing\binary", FileMode.Open, FileAccess.Read);
BinaryFormatter fb = new
BinaryFormatter();
emp = (Employee)fb.Deserialize(fs);
label5.Text = emp.getEmpNo();
label6.Text = emp.getName();
label7.Text = emp.getDepartment();
label8.Text=Convert.ToString(emp.getSalary());
}
//Soap Button click event
private void
button3_Click(object sender, EventArgs e)
{
Employee emp = new
Employee();
FileStream fs = new
FileStream(@"C:\Users\Harsha\Documents\Visual
Studio 2010\Projects\lab2\Serializing\Soap", FileMode.Open, FileAccess.Read);
SoapFormatter sp = new
SoapFormatter();
emp = (Employee)sp.Deserialize(fs);
label5.Text = emp.getEmpNo();
label6.Text = emp.getName();
label7.Text = emp.getDepartment();
label8.Text=Convert.ToString(emp.getSalary());
}
0 comments:
Post a Comment