/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); // Save Game Data SaveGameStorage storage = new SaveGameStorage(); SaveGame sg = new SaveGame(); sg.Name = "Ziggy"; sg.HiScore = 1000; sg.Date = DateTime.Now; sg.DontKeep = 123; storage.Save(sg); //load the data back in to test if it was successful SaveGame loaded = storage.Load(); Console.WriteLine("Name: " + loaded.Name); Console.WriteLine("Hi Score: " + loaded.HiScore.ToString()); Console.WriteLine("Date: " + loaded.Date.ToString()); Console.WriteLine("Dont Keep: " + loaded.DontKeep.ToString()); }
public SaveGame Load() { SaveGame ret = new SaveGame(); StorageDevice device = StorageDevice.ShowStorageDeviceGuide(); // Open a storage container StorageContainer container = device.OpenContainer("TestStorage"); // Get the path of the save game string filename = Path.Combine(container.Path, "savegame.xml"); // Check to see if the save exists if (!File.Exists(filename)) // Notify the user there is no save return ret; // Open the file FileStream stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Read); // Read the data from the file XmlSerializer serializer = new XmlSerializer(typeof(SaveGame)); ret = (SaveGame)serializer.Deserialize(stream); // Close the file stream.Close(); // Dispose the container container.Dispose(); return ret; }
/// <summary> /// Allows the game to perform any initialization it needs to before starting to run. /// This is where it can query for any required services and load any non-graphic /// related content. Calling base.Initialize will enumerate through any components /// and initialize them as well. /// </summary> protected override void Initialize() { // TODO: Add your initialization logic here base.Initialize(); // Save Game Data SaveGameStorage storage = new SaveGameStorage(); SaveGame sg = new SaveGame(); sg.Name = "Ziggy"; sg.HiScore = 1000; sg.Date = DateTime.Now; sg.DontKeep = 123; storage.Save(sg); //load the data back in to test if it was successful loaded = storage.Load(); }
public void Save(SaveGame sg) { StorageDevice device = StorageDevice.ShowStorageDeviceGuide(); // Open a storage container StorageContainer container = device.OpenContainer("TestStorage"); // Get the path of the save game string filename = Path.Combine(container.Path, "savegame.xml"); // Open the file, creating it if necessary FileStream stream = File.Open(filename, FileMode.OpenOrCreate); // Convert the object to XML data and put it in the stream XmlSerializer serializer = new XmlSerializer(typeof(SaveGame)); serializer.Serialize(stream, sg); // Close the file stream.Close(); // Dispose the container, to commit changes container.Dispose(); }