Export (0) Print
Expand All
3 out of 3 rated this helpful - Rate this topic

How To: Serialize Data

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.

The Complete Sample

The code in the topic shows you the technique. 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

  1. Define a new class or structure.

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

  2. Add the Serializable attribute to the class.

[Serializable]
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.

  2. Use Path.Combine to create a new string specifying the full path to the save game file.

  3. Open a FileStream object on the file using the File.Open method from the System.IO namespace.

    Specifying FileMode.Create tells Open to create the save game if it doesn't exist. If it does, the file will be truncated to zero bytes.

  4. Create an XmlSerializer object, passing the type of the structure that defines your save game data.

  5. Call Serialize, and then pass the FileStream and the data to serialize.

    The XmlSerializer will turn the data in the structure into XML and use the FileStream to write the data into the file.

  6. Close the FileStream.

  7. Dispose the StorageContainer to commit the changes to the device.

private static void DoSaveGame(StorageDevice device)
{
    // Create the data to save.
    SaveGameData data = new SaveGameData();
    data.PlayerName = "Hiro";
    data.AvatarPosition = new Vector2(360, 360);
    data.Level = 11;
    data.Score = 4200;

    // Open a storage container.
    StorageContainer container =
        device.OpenContainer("StorageDemo");

    // Get the path of the save game.
    string filename = Path.Combine(container.Path, "savegame.sav");

    // Open the file, creating it if necessary.
    FileStream stream = File.Open(filename, FileMode.Create);
    

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

    // Close the file.
    stream.Close();

    // 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.

  2. Use Path.Combine to create a new string specifying the full path to the save game file.

  3. Call File.Exists to determine if the save game exists.

  4. Open a FileStream object on the file using the File.Open method from the System.IO namespace.

  5. Create an XmlSerializer object, and then pass the type of the structure that defines your save game data.

  6. Call Deserialize, and then pass the FileStream 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.)

  7. Close the FileStream.

  8. Dispose the StorageContainer.

Did you find this helpful?
(1500 characters remaining)
Thank you for your feedback

Community Additions

ADD
Show:
© 2014 Microsoft. All rights reserved.