I have this piece of code to deserialize a serialized list of Animals. However i get the End of Stream encountered before parsing was completed, error. Do any of you have a solution to this? I cant seem to figure it out by myself.
public void Load(String fileName)
{
if (fileName == null)
{
throw new ArgumentNullException("fileName");
}
String path = @"C:" + fileName;
if (fileName.Contains(@"\"))
{
path = fileName;
}
using (FileStream fileStream = File.OpenRead(path))
{
BinaryFormatter format = new BinaryFormatter();
object obj = format.Deserialize(fileStream);
if (obj.GetType() == typeof(List<Animal>))
{
List<Animal> Animals = (List<Animal>)format.Deserialize(fileStream);
}
}
}
public void Save(String fileName)
{
if (fileName == null)
{
throw new ArgumentNullException("fileName");
}
String path = @"C:" + fileName;
if (fileName.Contains(@"\"))
{
path = fileName;
}
using (FileStream fileStream = File.Open(path, FileMode.Create))
{
BinaryFormatter format = new BinaryFormatter();
format.Serialize(fileStream, Animals);
}
}