MrGravity.LevelSelect.CheckForSave C# (CSharp) Method

CheckForSave() public method

Checks if a save file for the game already exists - and loads it if so. Meant to be used solely on XBOX360. Should be used as soon as we know what PlayerIndex the "gamer" is using (IE right after the title screen). Note: if a save game does not exist the constructor for this class should have filled in the default values (0 stars, all but the first locked - on xbox our default xml file should always have the default values - we cannot save information to that file as it is a binary xnb file that can't be changed at run time) Do not call this until we know the PlayerIndex the player is using!
public CheckForSave ( PlayerIndex player ) : void
player PlayerIndex
return void
        public void CheckForSave(PlayerIndex player)
        {
            IAsyncResult result;
            if (!DeviceSelected)
            {
                result = StorageDevice.BeginShowSelector(player, null, null);
                result.AsyncWaitHandle.WaitOne();
                _device = StorageDevice.EndShowSelector(result);
                result.AsyncWaitHandle.Close();
                DeviceSelected = true;
            }

            result = _device.BeginOpenContainer("Mr Gravity", null, null);
            result.AsyncWaitHandle.WaitOne();
            _container = _device.EndOpenContainer(result);
            result.AsyncWaitHandle.Close();

            if (_container.FileExists("LevelList.xml"))
            {
                var stream = _container.OpenFile("LevelList.xml", FileMode.Open);
                var serializer = new XmlSerializer(typeof(SaveGameData));
                var data = (SaveGameData)serializer.Deserialize(stream);
                stream.Close();
                var i = 0;
                foreach (var xLevels in data.Savedata.Elements())
                {

                    foreach (var xLevelData in xLevels.Elements())
                    {
                        foreach (var xLevel in xLevelData.Elements())
                        {
                            if (xLevel.Name == XmlKeys.Unlocked)
                                _mLevels[i].Unlocked = xLevel.Value == XmlKeys.True;

                            if (xLevel.Name == XmlKeys.Timerstar)
                                _mLevels[i].TimerStar = Convert.ToInt32(xLevel.Value);

                            if (xLevel.Name == XmlKeys.Collectionstar)
                                _mLevels[i].CollectionStar = Convert.ToInt32(xLevel.Value);

                            if (xLevel.Name == XmlKeys.Deathstar)
                                _mLevels[i].DeathStar = Convert.ToInt32(xLevel.Value);

                        }
                        i++;
                    }
                }

            }
            _container.Dispose();
        }