[Serializable()]
public class SaveGame
{
public string Name;
public int Age;
}
///<summary>
/// This is the main type for your game
///</summary>
public class Game1 : Microsoft.Xna.Framework.Game
{
GraphicsDeviceManager graphics;
SpriteBatch spriteBatch;
public Game1()
{
graphics = new GraphicsDeviceManager(this);
Content.RootDirectory = "Content";
this.Components.Add(new GamerServicesComponent(this));
}
protected override void Initialize()
{
base.Initialize();
}
///<summary>
/// LoadContent will be called once per game and is the place to load
/// all of your content.
///</summary>
protected override void LoadContent()
{
// Create a new SpriteBatch, which can be used to draw textures.
spriteBatch = new SpriteBatch(GraphicsDevice);
IAsyncResult result = Guide.BeginShowStorageDeviceSelector(PlayerIndex.One, null, null);
StorageDevice device = Guide.EndShowStorageDeviceSelector(result);
StorageContainer container = device.OpenContainer("StorageDemo");
SaveGame mySave1 = new SaveGame();
SaveGame mySave2 = new SaveGame();
mySave1.Name = "SharpGames";
mySave1.Age = 3;
this.Save(mySave1, container);
mySave2 = this.Load(container);
if (mySave2 != null)
{
//Sucesso!
}
}
public void Save(SaveGame sg, StorageContainer container)
{
string filename = Path.Combine(container.Path, "demo.xml");
FileStream stream = File.Open(filename, FileMode.OpenOrCreate);
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
serializer.Serialize(stream, sg);
// Close the file
stream.Close();
}
public SaveGame Load(StorageContainer container)
{
SaveGame saveGame = new SaveGame();
string filename = Path.Combine(container.Path, "demo.xml");
if (!File.Exists(filename))
{
return null;
}
FileStream stream = File.Open(filename, FileMode.OpenOrCreate, FileAccess.Read);
// Read the data from the file
XmlSerializer serializer = new XmlSerializer(typeof(SaveGame));
saveGame = (SaveGame)serializer.Deserialize(stream);
// Close the file
stream.Close();
return saveGame;
}
}