Showing posts with label XML. Show all posts
Showing posts with label XML. Show all posts

Tuesday, 29 October 2013

Serialize and DeSerialize of Class object to xml and Xml to Class

In this article we are going to see , how to serialize the class object to XML and De-Serialize the xml object to Class. For That Let we take a Example that , Employee file List ,which will maintain the information about employees and there projects.

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Serialization;
using System.IO;
using System.Reflection;

namespace ConsoleApplication8
{
    class Program
    {
        static void Main(string[] args)
        {
            Serialize();
            EmployeeFile EmpFile = DeSerialize();
            foreach (Employee emp in EmpFile.Employees)
                Console.WriteLine("Employee Name " + emp.Name);
            Console.Read();
        }

        static EmployeeFile DeSerialize()
        {
            XmlSerializer serialize=new XmlSerializer(typeof(EmployeeFile));
            TextReader reader=new StreamReader(@"D:\EmployeeFileList.xml",true);
            return (EmployeeFile)serialize.Deserialize(reader);
        }

        static void Serialize()
        {
            EmployeeFile employeefilelist = new EmployeeFile();
            Employee emp1 = new Employee();
            emp1.Name = "Suresh";
            emp1.Address = "chennai";
            emp1.Age = 22;
            emp1.Projects.Add(new Project() { Domain = "Java", Name = "Encryption Handling" });
            emp1.Projects.Add(new Project() { Domain = "DotNet", Name = "Automatic ReCycling" });

            Employee emp2 = new Employee();
            emp2.Name = "Rajesh";
            emp2.Address = "Chennai";
            emp2.Age = 25;
            emp2.Projects.Add(new Project() { Domain = "DotNet", Name = "Custom Controls" });
            emp2.Projects.Add(new Project() { Domain = "PHP", Name = "Faster Browsing" });


            employeefilelist.Employees.Add(emp1);
            employeefilelist.Employees.Add(emp2);

            XmlSerializer serialize = new XmlSerializer(typeof(EmployeeFile));
            TextWriter writer = new StreamWriter(@"D:\EmployeeFileList.xml", true);
            serialize.Serialize(writer, employeefilelist);
            writer.Close();

        }
     
    }

    [Serializable]
    public class Project
    {
        private string name;
        [XmlAttribute]
        public string Name
        {
            set { name = value; }
            get { return name; }
        }

        private string domain;

        [XmlAttribute]
        public string Domain
        {
            set
            {
                domain = value;
            }
            get
            {
                return domain;
            }
        }
    }

    [Serializable]
    public class Employee
    {
        private string name;
       
        [XmlAttribute]
        public string Name
        {
            set { name = value; }
            get { return name; }
        }

        private string address;

        [XmlAttribute]
        public string Address
        {
            set { address = value; }
            get { return address; }
        }

        private int age;
       
        [XmlAttribute]
        public int Age
        {
            set { age = value; }
            get { return age; }
        }

        private List<Project> projects = new List<Project>();

        public List<Project> Projects
        {
            set { projects = value; }
            get { return projects; }
        }

    }

    [Serializable]
    public class EmployeeFile
    {
        private List<Employee> employees = new List<Employee>();

        public List<Employee> Employees
        {
            set { employees = value; }
            get { return employees; }
        }

    }

}


 Output :

Serialize an Xml is Formed in Location D:\EmploeeeFileList.xml



DeSerialize the Xml and save the data in employeefile class object print the Employee name in Console:

Employee Name Suresh
Employee Name Rajesh


This article will explain you how to convert the xml to class object and from class object to xml.