4
Answers

XML serialize multiple classes

Marco

Marco

14y
3.3k
1
So I've got a bit of a problem I'm made a class that i have to use 7 times (one for each day of the week) I have the serialize set this way

 FileStream stream = new FileStream("C:\\employee.xml", FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(Day));
serializer.Serialize(stream, monday);
serializer.Serialize(stream, tuesday);
serializer.Serialize(stream, wednesday);
serializer.Serialize(stream, thursday);
serializer.Serialize(stream, friday);
serializer.Serialize(stream, saturday);
serializer.Serialize(stream, sunday);

problem is when I unserialize it only works if i only save the class once, is there anything that has to be done differently for more then 1 instance of the same class?

 FileStream stream = new FileStream("C:\\employee.xml", FileMode.Open);
XmlSerializer serializer = new XmlSerializer(typeof(Day));
monday = (Day)serializer.Deserialize(stream);

Thanks :)
Answers (4)