Exemple #1
0
        static void JsonSerialization()
        {
            Console.WriteLine("Json Formatter");
            Console.WriteLine("Crear un objeto y Guardarlo serializado");

            string filepath = "miObjeto.json";

            Comida pizza = new Comida()
            {
                Componente = "Maza, Queso, Salsa de Tomate, Tocino, Aceitunas", Nombre = "Especial"
            };
            string     json = JsonConvert.SerializeObject(pizza);
            FileStream fs   = new FileStream(filepath, FileMode.OpenOrCreate);

            using (StreamWriter sw = new StreamWriter(fs))
            {
                sw.WriteLine(json);
            }
            fs.Close();

            //Console.ReadKey();

            fs = new FileStream(filepath, FileMode.Open, FileAccess.ReadWrite);
            string jsonStorage;

            using (StreamReader sr = new StreamReader(fs))
            {
                jsonStorage = sr.ReadToEnd();
            }

            var c = JsonConvert.DeserializeObject <Comida>(jsonStorage);
            // Serializar JSON a XML y Viceversa
            var transform = JsonConvert.DeserializeXmlNode(jsonStorage, "Comida");
            var result    = JsonConvert.SerializeXmlNode(transform);

            // Serializar JSON a XML y Viceversa
            Console.WriteLine(c.Componente);
            Console.WriteLine(c.Nombre);
            fs.Close();
            File.Delete(filepath);
            Console.ReadKey();
        }