You are on page 1of 4

Serialization

Serialization is the process of converting an


object into a stream of bytes in order to store
the object or transmit it to memory, a
database, or a file.

Serialization can be performed in two ways :

Basic Serialization : . Basic serialization uses the .NET Framework to


automatically serialize the object.

Custom Serialization : In custom serialization, you can specify exactly


which objects will be serialized and how it will be done.

There are three types of serialization:

XmlSerialization (Save your object data into an XML file).

Binary serialization (Save your object data into binary format)

Soap Serialization (Save your object data into binary format; mainly
used in network related communication).

XML SERIALIZATION :

XML serialization only serializes public fields and properties.

XML serialization does not include any type information.

We need to have a default constructor in order to serialize an object.

Read-onlyproperties are not serialized.

ATTRIBUTES :

XmlAttribute : This member will be serialized as an xml attribute.

XmlElement : The field will be serialized as xml element.

XmlIgnore : The fields will be ignored while serialization.

XmlRoot : Represent XML documents root element.

We should include the following namespace :

using System.Xml.Serialization;

BINARY SERIALIZATION :

We use the following namespace for binary serialization :

usingSystem.Runtime.Serialization.Formatters.Binary;

To format the object as binary, we should include :

BinaryFormatterformatter =newBinaryFormatter();

Benefits of Binary Serialization :

It's the fastest serialization method because it does not have the overhead of generating
an XML document during the serialization process.

The resulting binary data is more compact than an XML string, so it takes up less storage
space and can be transmitted quickly.

Can serialize and restore non-public and public members of an object.

You might also like