1. Let me first define the Person Class
    using System;
    using System.Collections.Generic;
    using System.Linq;
    using System.Text;
    namespace zSharp2010.Serializer
    {
        [Serializable()]
        public class Person
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public DateTime DOB { get; set; }
            public string City { get; set; }
        }
    }

    The serialization class has two functions, Serializer and DeSerializer.
    We pass the xml file name and a list of person class to Serialize function to be serialized.
    The deserialized function returns a list of persons, we provide the xml file name.

    class cSerializer
    {
     public void Serialize(string file, List<Person> persons)
     {
      XmlSerializer xmlSerializer = new XmlSerializer(persons.GetType());
      StreamWriter writer = new StreamWriter(file);
      xmlSerializer.Serialize(writer, persons);
      writer.Close();
     }

     public List<Person> DeSerialize(string file)
     {
      List<Person> persons = new List<Person>();
      Person person = new Person();
      XmlSerializer xmlSerializer = new XmlSerializer(typeof (List<Person>));
      StreamReader reader = new StreamReader (file);
      persons = (List<Person>) xmlSerializer.Deserialize (reader);
      return persons;
     }
    }

    The calling methods might look like this:
    public partial class fSerializer : Form
    {
     string file = @"C:\temp\Serializer.xml";
     public fSerializer()
     {
      InitializeComponent();
     }

     private void cmdSerialize_Click(object sender, EventArgs e)
     {
      cSerializer serializer = new cSerializer();
      List<Person> persons = GetPersons();
      serializer.Serialize(file, persons);
     }

     private void cmdDeSerialize_Click(object sender, EventArgs e)
     {
      cSerializer serializer = new cSerializer();
      List<Person> persons = serializer.DeSerialize(file);
      string msg = String.Empty;
      foreach (Person person in persons)
       msg += String.Format("ID : {0}, Name : {1}, DOB : {2}, City : {3}\r\n", person.ID, person.Name, person.DOB, person.City);
      MessageBox.Show( msg);
     }

     private List<Person> GetPersons()
     {
      Person person = new Person();
      List<Person> persons = new List<Person>();
      person.ID = 1;
      person.Name = "George";
      person.DOB = Convert.ToDateTime("01-01-2001");
      person.City = "Mt. Vernon";
      persons.Add(person);
      person = new Person();
      person.ID = 2;
      person.Name = "John";
      person.DOB = Convert.ToDateTime("01-01-2002");
      person.City = "Quincy";
      persons.Add(person);
      person = new Person();
      person.ID = 3;
      person.Name = "Thomas";
      person.DOB = Convert.ToDateTime("01-01-2001");
      person.City = "Montecello";
      persons.Add(person);
      return persons;
     }
    }
    0

    Add a comment

Blog Archive
Topics
Topics
Loading
Dynamic Views theme. Powered by Blogger. Report Abuse.