public async Task SaveUserDataToFile(string filePath) { using (FileStream file = new FileStream(filePath, FileMode.Create)) { using (StreamWriter stream = new StreamWriter(file)) { //Create the DataWrapper object and add the apprpriate data XmlSerializer dataSerializer = new XmlSerializer(typeof(UserDataWrapper)); UserDataWrapper data = new UserDataWrapper(); data.Pokemon = this.MyPokemon; await Task.Run(() => dataSerializer.Serialize(stream, data)); //Saves the data using the attributes defined in each class } } }
/// <summary> /// Creates a new data file with no data in the current filepath /// </summary> public async Task CreateNewFile(string filePath) { //Debug.WriteLine("Creating new file at " + filePath); using (FileStream file = new FileStream(filePath, FileMode.Create)) { using (StreamWriter stream = new StreamWriter(file)) { //Create the DataWrapper object and add the apprpriate data XmlSerializer dataSerializer = new XmlSerializer(typeof(UserDataWrapper)); UserDataWrapper data = new UserDataWrapper(); //data.Moves = new MyObservableCollection<Move>(); //data.PokedexEntries = new MyObservableCollection<PokedexEntry>(); await Task.Run(() => dataSerializer.Serialize(stream, data)); //Saves the data using the attributes defined in each class } } }
public void Set(object value) { if (value == null) { RefVal = null; return; } if (value.GetType().IsValueType) { //the slow case SetFromValueType(value); return; } if (value is byte[] || value is Value[]) { value = new UserDataWrapper(value); } RefVal = value; }
/// <summary> /// Loads data from the current filepath /// </summary> public async Task LoadUserDataFromFile(string filePath) { this.MyPokemon.Clear(); UserDataWrapper data = new UserDataWrapper(); try { using (FileStream file = new FileStream(filePath, FileMode.Open)) { XmlSerializer dataSerializer = new XmlSerializer(typeof(UserDataWrapper)); data = await Task.Run(() => dataSerializer.Deserialize(file)) as UserDataWrapper; } } catch (IOException) //File does not exist; set everything to defaults { //await CreateNewFile(Properties.Settings.Default.DefaultDirectory + "\\data_new.xml"); } catch (InvalidOperationException ex) { Debug.WriteLine("Error reading xml:" + ex.Message); } List <Pokemon> tempPokemon = new List <Pokemon>(); foreach (Pokemon pokemon in data.Pokemon) { try { pokemon.Species = this.Pokedex.Single(x => x.Species.Equals(pokemon.Species.Species)); } catch (Exception) { throw new ArgumentException("Cannot find matching species " + pokemon.Species.Species + " in Pokedex."); } try { pokemon.FastMove = pokemon.Species.FastMoves.Single(x => x.FastMove.Name.Equals(pokemon.FastMove.FastMove.Name)); } catch (Exception) { if (pokemon.FastMove != null) { throw new ArgumentException($"Cannot find matching fast move {pokemon.FastMove.FastMove.Name} in fast move list for {pokemon.Species.Species}"); } else { throw new ArgumentException($"Cannot find matching fast move {pokemon.FastMove.FastMove.Name} in fast move list for {pokemon.Name}"); } } try { pokemon.ChargeMove = pokemon.Species.ChargeMoves.Single(x => x.ChargeMove.Name.Equals(pokemon.ChargeMove.ChargeMove.Name)); } catch (Exception) { if (pokemon.ChargeMove != null) { throw new ArgumentException($"Cannot find matching charge move {pokemon.ChargeMove.ChargeMove.Name} in charge move list for {pokemon.Species.Species}"); } else { throw new ArgumentException($"Cannot find matching charge move {pokemon.ChargeMove.ChargeMove.Name} in charge move list for {pokemon.Name}"); } } try { tempPokemon.Add(pokemon); } catch (Exception) { string temp = pokemon.Name; } } this.MyPokemon.InsertRange(tempPokemon); }