Hi I am wondering how I can keep the previous records with my stream writer, if I use the below code it works fine when the student record is created but when I create a second student record the previous record is gone? How can I keep all records?
public void AddStudent(Student student)
{
students.Add(student);
XmlSerializer s = new XmlSerializer(typeof(Student));
TextWriter w = new StreamWriter("c:\\list.xml");
s.Serialize(w, student);
w.Close();
}
EDIT UPDATE:
From the partial answer below I keep getting this error Type WcfServiceLibrary1.Student' in Assembly 'WcfServiceLibrary1, Version=1.0.0.0, Culture=neutral, PublicKeyToken=null is not marked as serializable
I have decorated the Student Class with [Serializable()] so im not sure whats going on?
Use this overload of StreamWriter’s constructor to append new data instead of overwriting.
Update:
I see, it works only with BinaryFormatter and not with XmlSerializer, because second write makes XML invalid. Unless you need XML format for some reason, using binary format is easier. This should work: