Serialization in C#
In this article I will demonstrate the use of serialization and how to perform XML serialization.
Serialization means saving the state of your object to secondary memory, such as a file.
Suppose you have a business layer where you have many classes to perform your business data.
Now suppose you want to test whether your business classes give the correct data out without verifying the result from the UI or from a database. Because it will take some time to process.
What to do to resolve the problem?
Here comes Serialization. You will serialize all your necessary business classes and save them into a text or XML file on your hard disk. So you can easily test your desired result by comparing your serialized saved data with your desired output data. You can say it is a little bit of autonomic unit testing performed by the developer.
There are three types of serialization
- Binary serialization (Save your object data into binary format)
- Soap Serialization (Save your object data into binary format; mainly used in network related communication).
- XmlSerialization (Save your object data into an XML file).
Here I will show you how to perform the XmlSerialization.
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading;
using System.Xml.Serialization;
using System.Runtime.Serialization.Formatters.Binary;
using System.IO;
namespace CSharpBasicCoading
{
public class A
{
public void Add()
{
Console.WriteLine("A");
}
public virtual void same()
{
Console.WriteLine("In A");
}
public virtual void foo()
{
Console.WriteLine("Foo A");
}
}
public class B :A
{
public void z()
{
Console.WriteLine("z");
}
public override void same()
{
Console.WriteLine("In B");
}
public new virtual void foo()
{
Console.WriteLine("Foo B");
}
}
public class C :B
{
public override void same()
{
Console.WriteLine("In C");
}
public override void foo()
{
Console.WriteLine("Foo C");
}
}
public class Test:C,IDisposable
{
public string FirstName;
public string MI;
public string LastName;
public void Dispose()
{
GC.SuppressFinalize(this);
}
}
class Program
{
static void Main(string[] args)
{
Test oc = new Test();
oc.FirstName = "shirendu";
oc.MI = "bikash";
oc.LastName = "Nandi";
XmlSerializer x = new XmlSerializer(oc.GetType());
Stream s = File.OpenWrite("c:\\test1.xml");
x.Serialize(s, oc);
oc.Dispose();
System.GC.Collect();
System.GC.WaitForPendingFinalizers();
Console.ReadLine();
}
}
}
Here in this program I have four classes.
The Test Class is inheriting all the "a","b" and "c" class.
The Test class also has three variables.
Here when you serialize the test class object in to the "test1.xml" file it saves the desired object result to the XML file.
Please run and test the program.
Here I have also used the garbage collector. It is good for a Professional program to destroy your object after using it. So in this article you can also get the idea of implement the garbage collector.
In my next article I will describe the Deserialization. It is nothing but the reverse engineering of serialization.