Mayar Akram
Mayar Akram

Reputation: 13

serialization multiple objects

I'm trying to make an application that allows people to register their information so that employers could read them and contact them.

The problem is whenever I try to deserialize the information, I either get one object only, or the program throw an exception.

private void button1_Click(object sender, EventArgs e)
{
    FileStream sw = new FileStream("Applicants.xml",FileMode.Append,FileAccess.Write,FileShare.None);
    XmlSerializer xs = new XmlSerializer(typeof(Class1), new XmlRootAttribute("Applist"));
    Class1 cc = new Class1();
    cc.name = textBox1.Text;
    cc.age = textBox2.Text;
    cc.degree = textBox3.Text;
    cc.salary = textBox4.Text;
    cc.no = textBox5.Text;
    c.Add(cc);

    xs.Serialize(sw,cc);


    this.Hide();
}

What should I do to serialize and deserialize all the objects created ? class1 :

public class Class1
{
    public String name;
    public String age;
    public String degree;
    public String no;
    public String salary;

}

deserialization code :

 private void linkLabel1_LinkClicked_1(object sender, LinkLabelLinkClickedEventArgs e)
 {
       List<Class1> c2 = new List<Class1>();
       XmlSerializer xml = new XmlSerializer(typeof(List<Class1>));
       FileStream fs = new FileStream("Applicants.xml",FileMode.Open);
       c2 = (List<Class1>)xml.Deserialize(fs);
       label3.Text = ; //don't know what t write here 
 }

Upvotes: 1

Views: 512

Answers (1)

CharithJ
CharithJ

Reputation: 47510

If you want to serialize the list, you have to create the Serializer for the type of List<Class1>.

XmlSerializer xs = new XmlSerializer(typeof(List<Class1>), new XmlRootAttribute("Applist"));

And then serialize the actual list instead of cc.

xs.Serialize(sw,c);

Upvotes: 2

Related Questions