problem with the deserializationserialisation of xml

The problem is with this line:

file.Write(streamer.GetBuffer(), 0, streamer.GetBuffer().Length);

You generally don’t want to access stream’s data via GetBuffer(), because it returns underlying byte array which can be larger than the data you wrote into the stream.

Either use stream.CopyTo method like this:

streamer.Seek(0, SeekOrigin.Begin); // dont forget to seek to the start of the stream
streamer.CopyTo(file);

Or simply write directly to the file, without using MemoryStream at all:

FileStream file = File.Create(Application.persistentDataPath + "/gamesave.save");
DataContractSerializer bf = new DataContractSerializer(newsave.GetType());
bf.WriteObject(file , newsave);
file.Close();

CLICK HERE to find out more related problems solutions.

Leave a Comment

Your email address will not be published.

Scroll to Top