예제 #1
0
        public static void SerializacionXml(string phantonPath, List <Fantasma> fantasmas)
        {
            // Serializacion Binaria
            var phantonXml = $"{phantonPath}\\phanton.xml";

            if (File.Exists(phantonXml))
            {
                XmlSerializer formatter = new XmlSerializer(typeof(List <Fantasma>));
                using (var buffer = File.OpenRead(phantonXml))
                {
                    fantasmas = formatter.Deserialize(buffer) as List <Fantasma>;
                    foreach (var g in fantasmas)
                    {
                        g.Asustar();
                        Console.WriteLine(g.ToString());
                    }
                }
            }
            using (var file = File.OpenWrite(phantonXml))
            {
                XmlSerializer formatter = new XmlSerializer(typeof(List <Fantasma>));
                var           fantasma  = new Fantasma()
                {
                    Nombre = "Fantasma-" + Path.GetRandomFileName(),
                };
                foreach (var g in fantasmas)
                {
                    fantasma.Asustar();
                }
                fantasmas.Add(fantasma);
                formatter.Serialize(file, fantasmas);
                file.Flush();
            }
        }
예제 #2
0
        public static void SerializacionJson(string phantonPath, List <Fantasma> fantasmas)
        {
            // Serializacion Json
            var phantonJson = $"{phantonPath}\\phanton.json";

            if (File.Exists(phantonJson))
            {
                using (var buffer = File.OpenRead(phantonJson))
                {
                    using (var streamReader = new StreamReader(buffer))
                    {
                        var texto = streamReader.ReadToEnd();
                        fantasmas = JsonConvert.DeserializeObject <List <Fantasma> >(texto);
                        foreach (var g in fantasmas)
                        {
                            g.Asustar();
                            Console.WriteLine(g.ToString());
                        }
                    }
                }
            }

            using (FileStream file = File.OpenWrite(phantonJson))
            {
                var fantasma = new Fantasma()
                {
                    Nombre = "Fantasma-" + Path.GetRandomFileName(),
                };
                foreach (var g in fantasmas)
                {
                    fantasma.Asustar();
                }
                fantasmas.Add(fantasma);
                var texto = JsonConvert.SerializeObject(fantasmas);
                using (var streamWriter = new StreamWriter(file))
                {
                    streamWriter.Write(texto);
                }
            }
        }
예제 #3
0
        public static void SerializacionBinaria(string phantonPath, List <Fantasma> fantasmas)
        {
            // Serializacion Binaria
            var phantonBinary = $"{phantonPath}\\phanton.file";

            // Deserializar
            if (File.Exists(phantonBinary))
            {
                IFormatter formatter = new BinaryFormatter();
                using (var buffer = File.OpenRead(phantonBinary))
                {
                    fantasmas = formatter.Deserialize(buffer) as List <Fantasma>;
                    foreach (var g in fantasmas)
                    {
                        g.Asustar();
                        Console.WriteLine(g.ToString());
                    }
                }
            }
            // Logica de Negocios
            var fantasma = new Fantasma()
            {
                Nombre = "Fantasma-" + Path.GetRandomFileName(),
            };

            foreach (var g in fantasmas)
            {
                fantasma.Asustar();
            }
            fantasmas.Add(fantasma);

            // Serializar
            using (var file = File.OpenWrite(phantonBinary))
            {
                IFormatter formatter = new BinaryFormatter();
                formatter.Serialize(file, fantasmas);
                file.Flush();
            }
        }