XNA Creators Club Online
Page 1 of 1 (7 items)
Sort Posts: Previous Next

Saving and loading error (XML)

Last post 25/01/2009 6:05 by Tomoprime. 6 replies.
  • 18/12/2008 9:39

    Saving and loading error (XML)

    Hi everyone. I've been getting an error relatively frequently that is really bizarre. I use XML serialize/deserialize for saving and loading my game, and every so often, the save file will become corrupt. There doesn't seem to be any link at all between turning the 360 off or quitting the game during saving. It has happened on PC and 360. I thought it had to do with what I had named some of the variables in my serializable class, but I changed their names and I got the error again, so I went into the file and investigated the error and found that the last line ended [/SaveGame]e], when it obviously should've just been [/SaveGame]. I didn't edit the file externally or anything. I left it all up to XML.Serialize. What's causing this problem, and how can I fix it?
    Funkmasonry Industries
    Purveyors of fine funk since 2007.
  • 18/12/2008 11:41 In reply to

    Re: Saving and loading error (XML)

    It's weird ... you might want to try loading the file as soon as you have saved it, at least in debug builds. It's a simple and effective way to ensure that your save/load code actually works. Maybe this way you have a better chance of catching this bug.

    The only thing i can think of is that if you're using XmlWriter you should probably make sure to call Dispose() on it once you're done.
    Since it happens on the 360 as well (have you checked if it's the same kind of error, for example by dumping the XML file on screen or transmitting it over the network?) i don't think it has to do with disk drive defects, a virus, or other such odd things. From the nature of the bug i wouldn't be surprised if it's got to do with multithreading. Are you by any chance using multithreading and write the same file from multiple threads without ensuring that only one thread can write the file at any given time?

  • 18/12/2008 11:45 In reply to

    Re: Saving and loading error (XML)

    Check the following in your code: When you open the file for writing, do you open it with FileMode.OpenOrCreate or with FileMode.Create? Because the former (OpenOrCreate) will have the effect, that if the file already exists, it will simply be opened (without deleting the existing content). Then if the data that you write is shorter than the data that was written before, the additional bytes of the old data will still be part of the new file (at the end). Which will break the XML syntax during loading, as there are additional characters at the end. You need to use FileMode.Create, which will have the effect, that if the file does not exist, it will be created, and if it already exists, it will be overwritten.
    The problem is, that even the serialization example in the XNA docs contains this bug, so many people probably transfer the bug from there to their own code (see here, where the bug is contained in the sample code: http://msdn.microsoft.com/en-us/library/bb203924.aspx).

    Doc

    EDIT: I now submitted this to connect.
    Please consider playtesting my game: Your Doodles Are Bugged!

    Twitter - My Game Trailers - www.spyn-doctor.de - Games: Kuchibi, Golden Tangram

    Useful for peer reviews and testing your own game: My little "evil" checklist for peer review stress testing
  • 18/12/2008 17:58 In reply to

    Re: Saving and loading error (XML)

    Thanks for your help. I did some quick testing, and you're right! If I enter a high score name that's shorter than the one that my score replaced, the save becomes corrupted. So does using FileMode.Create completely solve the problem? It seems weird that such a simple fix exists, but if so, I'm all for it ;). Thanks again. Julian
    Funkmasonry Industries
    Purveyors of fine funk since 2007.
  • 18/12/2008 19:22 In reply to

    Re: Saving and loading error (XML)

    Barryman:
    So does using FileMode.Create completely solve the problem?


    As far as I can tell, yes. I never had this problem again after I switched from FileMode.OpenOrCreate to FileMode.Create.

    If you check the docs on FileMode, it also becomes obvious that OpenOrCreate is definitely the wrong choice for serializing into an existing XML file, where the new data may be shorter than the already existing data, thus leaving trailing characters from the previous save at the end of the file after the characters of the newly saved XML data.

    Doc
    Please consider playtesting my game: Your Doodles Are Bugged!

    Twitter - My Game Trailers - www.spyn-doctor.de - Games: Kuchibi, Golden Tangram

    Useful for peer reviews and testing your own game: My little "evil" checklist for peer review stress testing
  • 18/12/2008 21:08 In reply to

    Re: Saving and loading error (XML)

    You shouldn't overwrite an existing file, ever. Instead, you should write to a temporary file, and then replace the destination file with the temporary file.
    This is known as a "safe save."

    Jon Watte, Direct3D MVP
    Tweets, occasionally
    kW X-port 3ds Max .X exporter
    kW Animation source code
  • 25/01/2009 6:05 In reply to

    Re: Saving and loading error (XML)

    Hi Folks

    I just ran into a similar problem however I added an extra line of code to truncate those extra characters.

    Hope that helps when editing in place ;-)

    // save changes to the stream
    serializer.Serialize(stream, data);
    // shorten the stream to the last known position & truncate any extra chars 8+)
    stream.SetLength(stream.Position);
    // Close the file.
    stream.Close();
    // commit changes to disk
    container.Dispose();

Page 1 of 1 (7 items) Previous Next