Serialization and Deserialization in C#.Net
In this article I will explain basic concept of serialization and desalinization with an example.
Here I will explain how to convert a csharp object to xml and vice-verse.
But why do I need to Serialize them?
The below example answers the above question.
1.I have a custom class which represents data about city
Students Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace SysDiagnosis
{
public class Student
{
public string Name { get; set; }
public string ID { get; set; }
public int Age { get; set; }
public string Gender { get; set; }
public double Grade { get; set;}
}
}
2.I instantiate the class and assign some values to the properties
SerilizeHelper Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
namespace SysDiagnosis
{
public class SerializeHelper
{
public static void SerializeMe(object obj, string strPath)
{
XmlSerializer serilizser = new XmlSerializer(obj.GetType());
StreamWriter writter = new StreamWriter(strPath);
serilizser.Serialize(writter.BaseStream,obj);
}
}
}
3.Below is the serialization code , The XmlSerializer and Serializer are used to do Serialization.
Main Program
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Management;
namespace SysDiagnosis
{
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = "Vinodh";
student.ID = "EE01";
student.Age = 27;
student.Gender = "Male";
student.Grade = 7.50;
Console.WriteLine("Serializing object to XML");
SerializeHelper.SerializeMe(student,@"C:\students.xml");
Console.ReadLine();
}
}
}
4.The output data will look like this
<?xml version="1.0"?>
<Student xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
<Name>Vinodh</Name>
<ID>EE01</ID>
<Age>27</Age>
<Gender>Male</Gender>
<Grade>7.5</Grade>
</Student>
5.In Deserialization we we use XmlSerializer object and Deserialize method, The StreamReader reads the xml data.
class Program
{
static void Main(string[] args)
{
Student student = new Student();
student.Name = "Vinodh";
student.ID = "EE01";
student.Age = 27;
student.Gender = "Male";
student.Grade = 7.50;
Console.WriteLine("Serializing Object to XML");
SerializeHelper.SerializeMe(student, @"C:\students.xml");
Console.WriteLine("Serializing XML to Object");
SerializeHelper.SerializeMe(@"C:\students.xml", new Student());
}
}
6. The below is DeSerialization code.
SerilizeHelper Class
public static void SerializeMe(string strPath, Student obj)
{
XmlSerializer serilizer = new XmlSerializer(obj.GetType());
StreamReader reader = new StreamReader(strPath);
object deserialize = serilizer.Deserialize(reader.BaseStream);
obj = deserialize as Student;
Console.WriteLine(obj.Name);
Console.WriteLine(obj.ID);
Console.WriteLine(obj.Age);
Console.WriteLine(obj.Gender);
Console.WriteLine(obj.Grade);
}
7. You can see the output in console:
Nice Article...Thanks.