pabloar93

serializacion xml

Sep 11th, 2013
45
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 1.37 KB | None | 0 0
  1. using System;
  2. using System.IO;
  3. using System.Collections.Generic;
  4. using System.Xml.Serialization;
  5. using System.Xml;
  6. using System.Text;
  7.  
  8. namespace SerializacionXML
  9. {
  10.     class MainClass
  11.     {
  12.         public static void Main (string[] args)
  13.         {
  14.             List<Persona> _listaPersonas = new List<Persona>()
  15.             {
  16.                 new Persona("Juan Roman","Riquelme",35),
  17.                 new Persona("Lionel","Messi",26),
  18.                 new Persona("Diego Armando","Maradona",52)
  19.             };
  20.             Persona pers = new Persona("Pablo","Raissiguier",20);
  21.             Serializar(_listaPersonas);
  22.             SerializarObjeto(pers);
  23.         }
  24.         public static void Serializar(List<Persona> lista)
  25.         {
  26.             XmlSerializer fl = new XmlSerializer(typeof(List<Persona>));
  27.             XmlTextWriter formateador = new XmlTextWriter("/home/pablo/Serializacion/datosLista.xml",Encoding.UTF8);
  28.             formateador.Formatting = Formatting.Indented;
  29.             formateador.Indentation = 1;
  30.             formateador.IndentChar = '\t';
  31.             fl.Serialize(formateador,lista);
  32.             formateador.Close();
  33.         }
  34.         public static void SerializarObjeto(Persona per)
  35.         {
  36.             XmlSerializer fl = new XmlSerializer(per.GetType());
  37.             XmlTextWriter formateador = new XmlTextWriter("/home/pablo/Serializacion/datosPersona.xml",Encoding.UTF8);
  38.             formateador.Formatting = Formatting.Indented;
  39.             formateador.Indentation = 1;
  40.             formateador.IndentChar = '\t';
  41.             fl.Serialize(formateador,per);
  42.             formateador.Close();
  43.         }
  44.     }
  45. }
Advertisement
Add Comment
Please, Sign In to add comment