Go to Comments





Serialization in .Net (C#) - Just sample code


Created: 12/06/2013 09:15:43 a.m.





Serialization in .Net (C#) - Just sample code

This is just a sample code to serialize and deserialize some object in c#

---
In this totorial you will see how to use serialization in .NET, using C#
---

FORM



Includes:
using System.Xml.Serialization;
using System.Runtime.Serialization;
using System.Runtime.Serialization.Formatters.Binary;


XML Serialization
----------------------------------------------------------------------

Object to Serialize (XML)
    public class Persona
    {
        private string _nombre;
        public string Nombre
        {
            get { return this._nombre; }
            set { this._nombre = value; }
        }

        private string _apellido;
        public string Apellido
        {
            get { return this._apellido; }
            set { this._apellido = value; }
        }

        private string _edad;
        public string Edad
        {
            get { return this._edad; }
            set { this._edad = value; }
        }
    }

Path XML File
private const string RUTA = @"C:\Documents and Settings\L0796891\Escritorio\ser.xml";

XML Serialization
        private void button1_Click(object sender, EventArgs e)
        {
            // Serialize
            Persona per = new Persona();
            per.Nombre = txtNombre.Text;
            per.Apellido = txtApellido.Text;
            per.Edad = txtEdad.Text;

            XmlSerializer serz = new XmlSerializer(typeof(Persona));
            StreamWriter sw = new StreamWriter(RUTA);

            serz.Serialize(sw, per);
            sw.Close();
        }

XML Deserialization
        private void button2_Click(object sender, EventArgs e)
        {
            // Deserealizar
            Persona per;
            XmlSerializer serz = new XmlSerializer(typeof(Persona));
            FileStream fs = new FileStream(RUTA, FileMode.Open);

            per = (Persona)serz.Deserialize(fs);

            txtNombre.Text = per.Nombre;
            txtApellido.Text = per.Apellido;
            txtEdad.Text = per.Edad;
            fs.Close();
        }



Binary Serialization
----------------------------------------------------------------------

Object to Serialize (Binary)
    [Serializable()]
    public class PersonaBin
    {
        private string _nombre;
        public string Nombre
        {
            get { return this._nombre; }
            set { this._nombre = value; }
        }

        private string _apellido;
        public string Apellido
        {
            get { return this._apellido; }
            set { this._apellido = value; }
        }

        private string _edad;
        public string Edad
        {
            get { return this._edad; }
            set { this._edad = value; }
        }
    }


Path XML File
private const string RUTA2 = @"C:\Documents and Settings\L0796891\Escritorio\ser.bin";

Binary Serialization
        private void button3_Click(object sender, EventArgs e)
        {
                // Binary serialization
                PersonaBin per = new PersonaBin();
                per.Nombre = txtNombre.Text;
                per.Apellido = txtApellido.Text;
                per.Edad = txtEdad.Text;

                IFormatter fmtr = new BinaryFormatter();

                Stream stream = new FileStream(RUTA2, FileMode.Create, FileAccess.Write, FileShare.None);
                fmtr.Serialize(stream, per);
                stream.Close();
        }

Binary Deserialization
        private void button2_Click(object sender, EventArgs e)
        {
            // Binary deserealization
            Persona per;
            XmlSerializer serz = new XmlSerializer(typeof(Persona));
            FileStream fs = new FileStream(RUTA, FileMode.Open);

            per = (Persona)serz.Deserialize(fs);

            txtNombre.Text = per.Nombre;
            txtApellido.Text = per.Apellido;
            txtEdad.Text = per.Edad;
            fs.Close();
        }





Comments: