If what you have serialized is the List of Alarm
s (and not a series of individual Alarm
s), which is more than probable, then you only have to deserialize that:
using (FileStream stream = File.OpenRead("//my File here"))
{
list = (List<Alarm>)serializer.Deserialize(stream));
}
Otherwise, if you have serialized standalone Alarm
objects, your code should work (each of them would be an object graph by itself, which seems not to be the case)
Edit: everything in what you posted seems to be fine. I've added a fiddle here ( https://dotnetfiddle.net/bGWOOO ) which demonstrate how the method you are using works (I used a MemoryStream
instead of a FileStream
for obvious reasons, but it should not matter). So there's something else wrong: either the stream is not being correctly written to disk, or not being correctly read, but it's not about the code you posted, definitely.
Update
Just for further reference as to why this answer was chosen (as the real answer was in the comments/chat):
In the serializer, you are creating the file everytime and replacing the contents (not shown in the question, but guessed out in the comments), so the answer was correctly appending to the file, something like this:
using (FileStream stream = new FileStream("** path to file here**", FileMode.Append, FileAccess.Write, FileShare.Write))
{
// write the serialized data to the stream
}
Best Answer
Try to set the position to 0 of your stream and do not use your object but the object type.