0
\$\begingroup\$

To followup from a previous query - I need some help with the implementation of easystorage high scores, which is bringing up some errors on the xbox.

I get the prompt screen, a savedevice is selected and a file are all created! However the file remains empty, (I've tried prepopulating but still get errors).

The full portions of the scoring code can be found here: http://pastebin.com/74v897Yt

The current issue in particular is in LoadHighScores() - "There is an error in XML document (0, 0)." under line data = (HighScoreData)serializer.Deserialize(stream);

I'm not sure whether this line is correct either: HighScoreData data = new HighScoreData();

public static HighScoreData LoadHighScores(string container, string filename) { HighScoreData data = new HighScoreData();

        if (Global.SaveDevice.FileExists(container, filename))
                {
                    Global.SaveDevice.Load(container, filename, stream =>
                            {
                                File.Open(Global.fileName_options, FileMode.OpenOrCreate,
                                         FileAccess.Read);  
                                try    
                            {         
                                            // Read the data from the file
                                        XmlSerializer serializer = new XmlSerializer(typeof(HighScoreData));
                                        data = (HighScoreData)serializer.Deserialize(stream);    
                            }    
                              finally
                             {      
                                    // Close the file  

                                stream.Close();
                             //   stream.Dispose();

                             }   

                            });

                }

        return (data);
    }

I call: PromptMe(); when the Start button is pressed at the beginning.

I call: if (Global.SaveDevice.IsReady){entries = LoadHighScores(HighScoresContainer, HighScoresFilename);} during the menu screen to try and display the highscore screen.

I call: SaveHighScore(); when game ends.

I've tried altering the struct code to a class but still no luck. Any help greatly appreciated.

\$\endgroup\$
1
  • \$\begingroup\$ Anyone any ideas? \$\endgroup\$
    – user1003211
    Commented Oct 26, 2011 at 13:21

2 Answers 2

1
\$\begingroup\$

You have modified the SaveHighScores from the original source to open or create the XML file instead of always overwriting it, which leads to interesting errors when loading.

Say you play your game, save high scores, and the resulting XML file is 100 bytes in length. You then play again and overwrite the high scores, but this time the XML content is only 99 bytes in length (for whatever reason the data is shorter, smaller score, etc). Using open or create the serializer will write out 99 bytes, leaving that 1 last junk byte from the first save at the end of the XML file. This leads to XML parse errors the next time you try and load the scores, just like you are seeing. If you manually inspect the file contents when debugging my guess is you will see junk trailing your XML from this exact scenario.

You will have to delete the file and change the SaveHighScores method back to always create instead of open or create. This will ensure that the old file is always deleted prior to saving, avoiding this issue.

\$\endgroup\$
1
\$\begingroup\$

dadoo Games hit the nail right on the head. Your problem is that you may be saving a smaller file into a larger one, causing many problems and headaches as you do so.

I second deleting your current save before you make a new one, so that you always have a fresh file. Doing so will save you many, many headaches and, while not the best method, it is certainly the easiest.

If you're worried about ending up with corrupted saves, you could always make a copy of your current save before deleting it. That way if it gets corrupted in any way, you can fall back on the older one.

So, the long and short of it when saving:

  1. (Optional) Back up your current save.
  2. Delete current save.
  3. Create a new save with your current data.
  4. (Optional) Check what saved against what you have on hand, to check if it saved correctly.

Doing so will ensure that the data you save will A.) Never crash due to having junk bytes and B.) You always correctly save your data. A bit of extra work with my optional steps, but it helps in keeping users happier if it corrupts (as they don't lose everything) and it helps prevent corruption by checking if it saved correctly immediately afterwards.

\$\endgroup\$

You must log in to answer this question.