Monday, April 7, 2014

XML serialization with C#

According to wikipedia:

"Serialization means converting an object into a data stream and writing it to storage. Any applet, application, or tool that uses that object or bean can then "reconstitute" it by deserialization. The object is then restored to its original state."

Serialization is a process that converts an object into a "bytes secuency" and those bytes can be stored or transmitted to convert that secuency into an object again.

Serialization is also known as deflating or marshall and the oposite process is also known as inflating or unmarshall.

Our example uses an "Automovil" class wich has these attributes:
-Marca [Brand]
-Modelo [Model]
-Año de fabricación [Year]
-Dirección hidráulica [Power steerging]
-Automático [Automatic]
-Placas [License plates]
-Numero de Cilindros [Cylinders]
-Numero de puertas [Doors qty]
-Color [Color]

First, we add [Serializable] attribute to our class















This form will let us add data to our "vehiculo" class.

Clicking on "Serializa!!!" we create our object to serialize.

Once our object is created we need a XmlSerializer [xmlSer] object and a stream [strRdr] to store our result (this example uses MemoryStream [ms]).


And this is our example working: