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

Monday, October 28, 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 (Employeeemp in EmpFile.Employees)
                Console.WriteLine("Employee Name " + emp.Name);
            Console.Read();
        }

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

        static voidSerialize()
        {
            EmployeeFile employeefilelist = new EmployeeFile();
            Employee emp1 = newEmployee();
            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 = newEmployee();
            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 = newXmlSerializer(typeof(EmployeeFile));
            TextWriter writer = newStreamWriter(@"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 stringdomain;

        [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 stringaddress;

        [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 = newList<Project>();

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

    }

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

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

    }

}


 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.