Search code examples
arrayswindows-phone-7xnaisolatedstoragexmlserializer

Saving an array in Windows Phone 7 Xna


I need to save an array in my xna game for wp7. I was thinking I could use the XmlSerializer class but I don't have the slightest on how to. Would this be the appropriate approach? Any suggestions? thanks in advance.


Solution

  • This should do:

    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.
        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();
    
        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);
    
        // Create the file.
        Stream stream = container.CreateFile(filename);
    
        // 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();
    }
    
    private static void DoLoadGame(StorageDevice 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();
    
        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;
        }
    
        // Open the file.
        Stream stream = container.OpenFile(filename, FileMode.Open);
    
        // Read the data from the file.
        XmlSerializer serializer = new XmlSerializer(typeof(SaveGameData));
        SaveGameData data = (SaveGameData)serializer.Deserialize(stream);
    
        // Close the file.
        stream.Close();
    
        // Dispose the container.
        container.Dispose();
    
        // Report the data to the console.
        Debug.WriteLine("Name:     " + data.PlayerName);
        Debug.WriteLine("Level:    " + data.Level.ToString());
        Debug.WriteLine("Score:    " + data.Score.ToString());
        Debug.WriteLine("Position: " + data.AvatarPosition.ToString());
    }