예제 #1
0
        public void Save(string fileName, Pokedex pokedex)
        {
            string     outputFile = fileName + ".xml";
            FileStream fs;

            if (File.Exists(fileName))
            {
                fs = File.Open(outputFile, FileMode.Append);
            }
            else
            {
                fs = File.Open(outputFile, FileMode.Create);
            }
            XmlSerializer serializer = new XmlSerializer(typeof(Pokedex));

            serializer.Serialize(fs, pokedex);
            fs.Close();
        }
예제 #2
0
 public void Save_Pokedex(Pokedex dex, string fileName)
 {
     //TODO:: check if file end of .xml DONE
     if (!fileName.EndsWith(".xml"))
     {
         fileName = fileName + ".xml";
     }
     if (File.Exists(fileName))
     {
         throw new Exception(string.Format("File {0} already exist, " +
                                           "overwrite a existing pokedex is not allowed, " +
                                           "the pokedex onwer going to be upset", fileName));
     }
     using (FileStream fs = new FileStream(fileName, FileMode.Create))
     {
         pokedexSerializer.Serialize(fs, dex);
     }
 }
예제 #3
0
        public void PokemonReader_Load_Pokedex_HappyPath()
        {
            Pokedex expect = new Pokedex();
            Pokedex actrual;

            // set up expect
            Pokemon thePokemon = new Pokemon
            {
                Index   = 1,
                Name    = "Bulbasaur",
                Type1   = "Grass",
                Type2   = "Poison",
                HP      = 128,
                Attack  = 118,
                Defense = 111,
                MaxCP   = 1115
            };

            expect.Pokemons.Add(thePokemon);


            // Create a simple test xml for Pokedex

            string fileName = "testUse_pokedex.xml";

            path += fileName;

            using (FileStream fs = new FileStream(path, FileMode.Create))
            {
                using (StreamWriter sw = new StreamWriter(fs))
                {
                    sw.WriteLine(pokedex_sourceExample_XML);
                }
            }

            actrual = testReader.Load_Pokedex(path);

            Assert.AreEqual(expect, actrual);
        }
예제 #4
0
        /// <summary>
        /// Load a xml file that contains Pokemon Data to be deserialized into a list of Pokemons
        /// </summary>
        /// <param name="filepath">The location of the xml file</param>
        /// <returns>A list of Pokemons</returns>
        public Pokedex Load(string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new Exception($"{filepath} does not exist");
            }

            Pokedex dex = null;

            using (var file = new StreamReader(filepath))
            {
                try
                {
                    dex = new XmlSerializer(typeof(Pokedex)).Deserialize(file) as Pokedex;
                }
                catch (Exception ex)
                {
                    throw new Exception($"Unable to deserialize the {filepath} due to following: {ex.Message}");
                }
            }

            return(dex);
        }
예제 #5
0
        /// <summary>
        /// Load a xml file that contains Pokemon Data to be deserialized into a list of Pokemons
        /// </summary>
        /// <param name="filepath">The location of the xml file</param>
        /// <returns>A list of Pokemons</returns>
        public Pokedex LoadPokedex(string filepath)
        {
            if (!File.Exists(filepath))
            {
                throw new Exception(string.Format("{0} does not exist", filepath));
            }

            Pokedex dex = null;

            using (var file = new StreamReader(filepath))
            {
                try
                {
                    dex = serializer.Deserialize(file) as Pokedex;
                }
                catch (Exception ex)
                {
                    throw new Exception(string.Format("Unable to deserialize the {0} due to following: {1}",
                                                      filepath, ex.Message));
                }
            }
            return(dex);
        }
예제 #6
0
 public void LoadPokemonFile_HappyPath()
 {
     dex = reader.Load(fileName);
 }