Vijay

Vijay

  • NA
  • 150
  • 90.4k

Create XML Serialization/Deserialization using memorystream?

Jul 22 2013 8:50 AM
How to create a XML Serialiazation/Deserialization for generic list in
C# windows form application?


//Create a Class


 public class Personal
        {
            public int ID { get; set; }
            public string Name { get; set; }
            public string Address { get; set; }
            public long Phone { get; set; }
        }


//Created a Serialiazation


private void Serialize_Click(object sender, EventArgs e)
        {
            Personal per = new Personal();
            per.ID = 1;
            per.Name = "John";
            per.Address = "Newyork";
            per.Phone = 48721522;            
            txtsno.Text = Convert.ToString(per.ID);
            txtname.Text = per.Name;
            txtaddr.Text = per.Address;
            txtpno.Text = Convert.ToString(per.Phone);
            XmlSerializer xmlser = new XmlSerializer(typeof(Personal));
            StreamWriter swtr = new StreamWriter(@"d:\serialization.xml");
            xmlser.Serialize(swtr, per);
            swtr.Close();          
        }


//Created a Deserialiazation


private void Deserialize_Click(object sender, EventArgs e)
        {
            XmlSerializer xmlser = new XmlSerializer(typeof(Personal));
            StreamReader srdr = new StreamReader(@"d:\serialization.xml");
            Personal p = (Personal)xmlser.Deserialize(srdr);
            srdr.Close();
            txtsno.Text = Convert.ToString(p.ID);
            txtname.Text = p.Name;
            txtaddr.Text = p.Address;
            txtpno.Text = Convert.ToString(p.Phone);            
        }


Here i want to create Generic Collections type like


            List<Personal> PList = new List<Personal>();
Personal per1 = new Personal();
Personal per2 = new Personal();
Personal per3 = new Personal();


PList.Add(per1);
PList.Add(per2);
PList.Add(per3);


I want to access anyone from lists(like per11,per2, per3...) dynamically while i use both  serialize and deserialize...


Ex.. in DeSerialize i will get anyone list i.e per3 ....and i display it on textfields in WFA


I want to write program like this....could i get me this......








Answers (4)