In this article I would like to demonstrate a generic XML Serialization/Deserialization process.
There are many situations where we need to convert class objects to XML and XML to class objects in real-time projects.
For the demonstration I have two classes named Employee and Address.
Employee
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomSerilization
{
public class Employee
{
public int ID { get; set; }
public string Name { get; set; }
public string Profession { get; set; }
}
}Address
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
namespace CustomSerilization
{
public class Address
{
public int HouseNo { get; set; }
public string StreetName { get; set; }
public string City { get; set; }
public string Country { get; set; }
}
}I have created a generic Serialization/Deserialization class that performs the main activities as in the following:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace CustomSerilization
{
public class SerializeDeserialize<T>
{
StringBuilder sbData;
StringWriter swWriter;
XmlDocument xDoc;
XmlNodeReader xNodeReader;
XmlSerializer xmlSerializer;
public SerializeDeserialize()
{
sbData = new StringBuilder();
}
public string SerializeData(T data)
{
XmlSerializer employeeSerializer = new XmlSerializer(typeof(T));
swWriter = new StringWriter(sbData);
employeeSerializer.Serialize(swWriter, data);
return sbData.ToString();
}
public T DeserializeData(string dataXML)
{
xDoc = new XmlDocument();
xDoc.LoadXml(dataXML);
xNodeReader = new XmlNodeReader(xDoc.DocumentElement);
xmlSerializer = new XmlSerializer(typeof(T));
var employeeData = xmlSerializer.Deserialize(xNodeReader);
T deserializedEmployee = (T)employeeData;
return deserializedEmployee;
}
}
}The SerializeDeserialize class is a generic class that accepts any input.
Main Class
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using CustomSerilization;
namespace GenericSerialization
{
class Program
{
static void Main(string[] args)
{
//Employee Serialization/Deserialization
Console.WriteLine("Generic Serialization/Deserialization");
Console.WriteLine("-----Serialized Employee Data-----");
Console.WriteLine("--------------------------------------------------------------------------------");
SerializeDeserialize<Employee> serializeEmployee;
Employee employee = new Employee { ID = 1001, Name = "Praveen Raveendran", Profession = "IT Services" };
serializeEmployee = new SerializeDeserialize<Employee>();
string serializedEmployee=serializeEmployee.SerializeData(employee);
Console.WriteLine("Serialized Employee Data : ");
Console.WriteLine(serializedEmployee);
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("-----Deserialized Employee Data-----");
Employee deserialiedEmployee = serializeEmployee.DeserializeData(serializedEmployee);
Console.WriteLine("Employee ID : {0} , Name :{1} ,Profession : {2}", deserialiedEmployee.ID, deserialiedEmployee.Name, deserialiedEmployee.Profession);
Console.WriteLine("--------------------------------------------------------------------------------");
//Address Serialization/Deserialization
Console.WriteLine("-----Serialized Address Data-----");
SerializeDeserialize<Address> serilaizeAddress;
Address address = new Address { HouseNo = 8911, StreetName = "Technopark", City = "Trivandrum", Country = "India" };
serilaizeAddress = new SerializeDeserialize<Address>();
string serailizedAddress = serilaizeAddress.SerializeData(address);
Console.WriteLine("Serialized Address Data : ");
Console.WriteLine(serailizedAddress);
Console.WriteLine("--------------------------------------------------------------------------------");
Console.WriteLine("-----Deserialized Address Data-----");
Address deserialiedAddress = serilaizeAddress.DeserializeData(serailizedAddress);
Console.WriteLine("House No :{0} , Street :{1} ,City : {2} ,Country: {3}", deserialiedAddress.HouseNo, deserialiedAddress.StreetName, deserialiedAddress.City, deserialiedAddress.Country);
Console.WriteLine("--------------------------------------------------------------------------------");
Console.ReadLine();
}
}
}By using this generic class it is possible to perform generic XML serialization/deserialization.
Output



Ananthu ChandranPosted Jul 13, 2019, 6:14 PM EDT
Hi, can you please explain how to serialize and deserialize the attributes in xml?
Praveen Raveendran PillaiPosted May 8, 2014, 9:24 PM EDT
I request all users to have an idea about XML classes in .Net before going through this article. There are lot of articles/blogs about XML classes and Generics in c-Sharpcorner.Please have a look it at for reference.
Gopi ChandPosted May 8, 2014, 3:45 AM EDT
Yes absolutely..but there are some users who are the beginners and they need some explaination and then only they can understand the concepts..
Praveen Raveendran PillaiPosted May 8, 2014, 12:18 AM EDT
For better understanding users need to know the XML classes in .Net.
Gopi ChandPosted May 7, 2014, 11:19 PM EDT
Nice article..but need more explaination for better understanding for learners..