SERIALIZATION and
DESERIALIZATION IN C#
What is Serialization?
Serialization is the process of converting an object into a form that can be readily transported. For
example, you can serialize an object and transport it over the Internet using HTTP between a client
and a server. On the other end, deserialization reconstructs the object from the stream.
Xml and Soap serialization
XML serialization converts (serializes) the public fields and properties of an object, or the parameters
and return 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. For example, XML Web services created using ASP.NET use the XmlSerializer
class to create XML streams that pass data between XML Web service applications throughout the
Internet or on intranets. Conversely, deserialization takes such an XML stream and reconstructs the
object.
XML serialization can also be used to serialize objects into XML streams that conform to the SOAP
specification. SOAP is a protocol based on XML, designed specifically to transport procedure calls
using XML.
How to serialize an Object in Xml format?
Step :1 Create new windows form application and name it as
DemoOfSerializationAndDeserialization as shown in the following picture.
Click OK button.
Step: 2 Design your form as shown below
Step :3 Now here we need to add one class. So right click on project in solution explorerClick on
AddClick on classand name it as Account.cs as shown below
Click on Add button.
Step : 4 Add following code in Account.cs file
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace DemoOfSerializationAndDeserialization
{
[Serializable]
public class Account
{
private string _name;
private double _balance;
public Account(string name, double balance)
{
_name = name;
_balance = balance;
}
public void Deposit(double amt)
{
_balance = _balance + amt;
}
public void withdraw(double amt)
{
_balance = _balance - amt;
}
public string Name
{
get
{
return _name;
}
}
public double Balance
{
get
{
return _balance;
}
}
}
}
In the above code we created two properties name and balance and along with that we have
created to method Deposit and Withdraw. When customer deposit the amount balance will increase
and when he withdrew amount balance will decrease.
Step : 4 Add two namespace in Form.cs file
using System.IO;
using System.Runtime.Serialization.Formatters.Soap;
On click event of serialize button write down the following code.
private void button1_Click(object sender, EventArgs e)
{
if (comboBox1.SelectedItem.ToString() == "Deposit")
{
int Amount = Convert.ToInt32(textBox2.Text);
Account acc = new Account(textBox1.Text, Amount);
acc.Deposit(Amount);
MessageBox.Show("current Name is" + acc.Name + "balance is" +
acc.Balance);
SoapFormatter myformatter = new SoapFormatter();
FileStream fs = new FileStream("C:\\Users\\ACER\\Desktop\\acc.xml",
FileMode.Create, FileAccess.Write);
myformatter.Serialize(fs, acc);
MessageBox.Show("object has been serialize");
}
else
{
int Amount = Convert.ToInt32(textBox2.Text);
Account acc = new Account(textBox1.Text, Amount);
acc.withdraw(Amount);
MessageBox.Show("current Name is" + acc.Name + "balance is" +
acc.Balance);
SoapFormatter myformatter = new SoapFormatter();
FileStream fs = new FileStream("C:\\Users\\ACER\\Desktop\\acc.xml",
FileMode.Create, FileAccess.Write);
myformatter.Serialize(fs, acc);
MessageBox.Show("object has been serialize");
}
Step :5 Press f5 to run your project . Provide necessary information in form and press Serialize
button. If everything goes fine , you will get MessageBox displaying "object has been serialize"
As shown below
Step: 6 Go to mention location of file. In my case it is Desktop. You will get ACC.xml file is created.
Open Acc.xm file , you will see the information stored in xmlsoap format as shown below
Step:7 For de-serializing object on click event of deserialize button write down the following code
private void button2_Click(object sender, EventArgs e)
{
SoapFormatter myformatter = new SoapFormatter();
FileStream fs = new FileStream("C:\\Users\\ACER\\Desktop\\acc.xml",
FileMode.Open, FileAccess.Read);
Account acc = (Account)myformatter.Deserialize(fs);
fs.Close();
MessageBox.Show("ur name is" + acc.Name + "u r balace is " + acc.Balance);
}
Step :8 press f5 to run your project and click on deserialize button. You will get information from
acc.xml serialized file as shown below
Ref: http://msdn.microsoft.com/en-us/library/90c86ass.aspx
Prashant Nimbare
Faculty
NIIT GT