XML Serialization and Deserialization in C#

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 

Output


Similar Articles