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:

output


Comments

Author: Rakesh Chaubey25 Apr 2013 Member Level: Gold   Points : 0

Nice Article...Thanks.

Author: Shine S25 Apr 2013 Member Level: Gold   Points : 4

as you said
"There is a need where I have to send data from .dot net application to other application which built out from java.
I cannot send .net objects to it , as it cannot understand. so i need to serilize the data to a common language where both of
them can understand., so I serialize my .net objects to xml and vice-versa as shown below"

can you explain how will you deserilize dotnet object in java?
how will java know the class structure?
what if you have used the binary serilization.

since this is a very small xml, why you select serilization for transferring data?
you can simply wrtie an xml file using xml writer and pass the file to other application.

Author: devin25 Apr 2013 Member Level: Gold   Points : 5

Hi , thanks for the comments.

We deserialize the dotnet object in .net code and send to the other platform app say java.

It depends when to use binary or xml serialization, say I want to write a simple chat application in .net(between to two dotnet app) , then i may use binary serialization, as I convert them to machine understandable language.

If I wish data manipulation to be easy,user-friendly and verbose kind then I go for xml serialization, but in other hand binary serialization is faster when compared to xml. Both the ways have pros and cons.

I have used very small xml just for newbies to understand.
We can use xml writer and parse them if the xml structure and size is small.
I have altered the post as per your comments. Thanks.

Author: Shine S25 Apr 2013 Member Level: Gold   Points : 4

i mean to say , one can deserilize an object only if it knows about the class which serialize the object. in the code for the serializer you have to give the type of the object.
XmlSerializer serilizer = new XmlSerializer(obj.GetType());
so my question was is it possible to deserilze an xml created in dotnet to an object in java app?

basically serialization is done to persist the data or object in memory.here you serialized the student object in memory to a file(xml) and stored in disk.
if it is stored as xml, it can be parsed between java and dotnet.
but if it was binary serialized, then i guess java app cannot deserilze the file.

Author: devin25 Apr 2013 Member Level: Gold   Points : 4

yes, XmlSerializer serilizser = new XmlSerializer(obj.GetType()); is needed, for understantability I am storing the xml in local disk. xml can be SOAP calls between cross apps.


this is just a simple application for newbies, i will explain in depth in forthcoming posting.

Altered the post as per your comments
Thanks.



  • Do not include your name, "with regards" etc in the comment. Write detailed comment, relevant to the topic.
  • No HTML formatting and links to other web sites are allowed.
  • This is a strictly moderated site. Absolutely no spam allowed.
  • Name:
    Email: