Export (0) Print
Expand All

Saving Data to a Save Game File

Demonstrates how to use the XmlSerializer class to write data in a custom class to a saved game, and how to load the data from the file.

Bb203924.note(en-us,XNAGameStudio.41).gifNote

The simplified example code demonstrates synchronous usage of BeginOpenContainer through the WaitOne method accessible through IAsyncResult.AsyncWaitHandle.

The preferred technique to use this function asynchronously is similar to what is demonstrated in the Getting a StorageDevice Asynchronously topic.

Complete Sample

The code in the topic shows you the technique for creating a save game file. You can download a complete code sample for this topic, including full source code and any additional supporting files required by the sample.

Serializing Data

To define save game data

Define a new class or structure.

The size of the class determines the size of your saved game files, so try to keep the class size small. Do not reference other objects from this class unless you also want to serialize them.

public struct SaveGameData
{
    public string PlayerName;
    public Vector2 AvatarPosition;
    public int Level;
    public int Score;
}

To serialize data to a save game file

  1. Create a StorageContainer to access the specified device.

    // Open a storage container.
    IAsyncResult result =
        device.BeginOpenContainer("StorageDemo", null, null);
    
    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();
    
    StorageContainer container = device.EndOpenContainer(result);
    
    // Close the wait handle.
    result.AsyncWaitHandle.Close();
    
  2. Call FileExists to determine if an earlier save game file exists, and if it does, call DeleteFile to delete it.

    string filename = "savegame.sav";
    
    // Check to see whether the save exists.
    if (container.FileExists(filename))
       // Delete it so that we can create one fresh.
       container.DeleteFile(filename);
    
  3. Create a Stream object on the file by using the CreateFile method.

    // Create the file.
    Stream stream = container.CreateFile(filename);
    
  4. Create an XmlSerializer object, passing the type of the structure that defines your save game data.

    // Convert the object to XML data and put it in the stream.
    XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
    
  5. Call Serialize, and then pass the Stream and the data to serialize.

    The XmlSerializer converts data in the structure to XML, and uses the Stream to write the data into the file.

    serializer.Serialize(stream, data);
    
  6. Close the Stream.

    // Close the file.
    stream.Close();
    
  7. Dispose the StorageContainer to commit the changes to the device.

    // Dispose the container, to commit changes.
    container.Dispose();
    

To read serialized data from a save game file

  1. Create a StorageContainer to access the specified device.

    // Open a storage container.
    IAsyncResult result =
        device.BeginOpenContainer("StorageDemo", null, null);
    
    // Wait for the WaitHandle to become signaled.
    result.AsyncWaitHandle.WaitOne();
    
    StorageContainer container = device.EndOpenContainer(result);
    
    // Close the wait handle.
    result.AsyncWaitHandle.Close();
    
  2. Call FileExists to determine if the save game exists.

    string filename = "savegame.sav";
    
    // Check to see whether the save exists.
    if (!container.FileExists(filename))
    {
       // If not, dispose of the container and return.
       container.Dispose();
       return;
    }
    
  3. Open a Stream object on the file by using the OpenFile method.

    // Open the file.
    Stream stream = container.OpenFile(filename, FileMode.Open);
    
  4. Create an XmlSerializer object, and then pass the type of the structure that defines your save game data.

    XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
    
  5. Call Deserialize, and then pass the Stream object.

    Deserialize returns a copy of the save game structure populated with the data from the save game file. (You will have to cast the return value from Object to your type.)

    SaveGameData data = (SaveGameData)serializer.Deserialize(stream);
    
  6. Close the Stream.

    // Close the file.
    stream.Close();
    
  7. Dispose the StorageContainer.

    // Dispose the container.
    container.Dispose();
    

Community Additions

ADD
Show:
© 2014 Microsoft