1

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 
 }
5
  • What is c ? List<Class1> ? Commented Jul 19, 2015 at 21:18
  • c is a list of class1 that contains fields (name,age,degree,salary) Commented Jul 19, 2015 at 21:26
  • 2
    it's seem that you serialize cc and not c. So you will get only cc, and not a list of c Commented Jul 19, 2015 at 21:40
  • if i serialize c , it throws an exception (error generating xml document ) Commented Jul 19, 2015 at 22:13
  • You need to: 1. Show the full definition of Class1, 2. show the code you are using to deserialize the XML and 3. detail the exception that you are getting. Commented Jul 19, 2015 at 22:42

1 Answer 1

2

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);
Sign up to request clarification or add additional context in comments.

5 Comments

that worked (Y) , but what about deserializtion , excuse me am still a beginner :'D
@MayarAkram: I am not sure what you want to show in the label3 Text. You have already managed to Deserialized it and got the list of Class1 instances. You can do what every you want from the list.
@MayarAkram: Please remember to select an answer for your question if there is any answer that solved your question.
I made label3.text to print all the serialized information again to show it all , but I can't
@MayarAkram: After deserializing you get a list of objects. You cannot show all objects in one label text.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.