Topics covered:

  • Serialization Overview
    • Serialization Advantages
  • Serialization Formatters
    • The [Serializable] Attribute
  • IDeserializationCallback Interface
  • XML Serialization

Video (in Bulgarian)

Presentation Content

What is Serialization?

  • Serialization
    • Process, which transforms object or linked graph of objects into a stream of bytes
    • The stream may be binary or text based (XML)
  • Deserialization
    • Process, which transforms a stream of bytes back to the original serialized objects

Serialization – Characteristics

  • Fully automated process in .NET Framework
  • Used internally in many .NET technologies
    • E.g. ASP.NET ViewState, WCF, RIA Services, …

Serialization – When is it Used?

  • Storing object’s state
    • A program can store its state in the memory, in a file, database, etc.
  • Transferring objects through the network
  • Cyclic graphs of objects (objects referencing each other) can be serialized too
    • The formatter serializes each object only once
    • Does not enter in a infinite loop

Internal Uses of Serialization in the .NET Technologies

  • .NET uses serialization internally for
    • Storing the session state and view state in ASP.NET
    • Deep object cloning
    • Transferring value type objects from one application domain to another
    • In the WCF technologies
  • Serialization is transparent for the developers
    • Does not need modifying the serialized object
  • After an object is transformed into a stream of bytes it can be transformed in many ways, e.g.
    • Encrypted
    • Compressed

Serialization – Advantages

  • Storing an object can be made manually, without serialization
    • This was done in the past (before .NET and Java)
  • Serialization makes the entire serialization / deserialization process:
    • Transparent
    • Automated
    • Controlled
  • Imagine the following:
    • Business application with more than 10 000 classes
    • Complex graph of objects in the application
    • Code in each class, which handles
      • Working with protocols
      • Client / server types differences
      • Error handling
  • Isn’t it better to have unified serialization / deserialization framework?

Serializing .NET Objects

  • The object which will be serialized can be of any type, e.g.
    • Int32 , String , DateTime , Exception , Image , Array <T> , List <T > , etc.
    • User defined class or structure
  • .NET has some special requirements for objects being serialized
  • The formatter is special class which formats the serialized data
    • Implements the IFormatter interface
  • A stream is used to store the serialized objects
  • Inheritor of System.IO.Stream
    • Objects can be serialized to MemoryStream , FileStream , NetworkStream , etc.
  • The serialization process relies on the formatter
    • The formatter iterates all object’s members recursively, using reflection
  • The scope modifiers do not matter
    • All elements are serialized (including private)
    • Static elements are not serialized


  • Formatters contains logic for formatting objects being serialized
    • CLR iterates the metadata for the members and extracts their values by reflection
    • These values are passed to the formatters
  • Standard formatters defined in the namespace System.Runtime.Serialization :
    • BinaryFormatter
    • SoapFormatter
  • Developers can define their own user-defined formatters
    • Inherit the Formatter class
    • Which implements IFormatter

Deep Copy

  • Deep copy is the operation that clones an object along with all its properties recursively
    • Deep copy == cloning of the entire object graph
    • Can be done using serialization


  • Sometimes we need to perform some things after an object have been deserialized
    • E.g. fill some transient fields with their default values
  • Just implement IDeserializationCallback and its method OnDeserialization ()
  • CLR executes this method after the deserialization of the object is completed

What is XML Serialization?

  • XML serialization
    • Storing the public fields of an object in XML format in order to save or transport them
      • Supported by the .NET XML API
      • Can serialize all types of objects
      • Cannot handle cyclic graphs of objects
      • User defined classes must have parameterless constructor
  • XML deserialization
    • The reverse process


  • Define classes Country and Town , which contains information about towns and countries. Consider that one country has many towns. Implement binary and XML serializations and deserializations for these classes.
  • Write a class called Graph , which describes directed graph, represented as list of neighbors for each vertex. Write a class Node , which describes a graph’s vertex. The class Node should contain data part (text field) and list of descendent nodes. Implement functionality to serialize and deserialize the instances of the class Graph .
  • Explain why Binary Formatter can serialize cyclic graph of objects, and XML serialization can not. Hint: use the Graph class written in exercises, make it cyclic and serialize it in the both ways and compare the output XML files.