..


Sponsored Links

Serialization in C #

Article written by Vincent Gaglio
Page 1 of 5

In certain circumstances, applications may have the need to store or transfer data contained in certain objects, to make these operations the simplest possible. NET Framework provides several techniques for serialization. These techniques allow you to convert objects into binary data, SOAP or XML through which to easily manage the storage and transfer of the same.

When you create an object in an application. NET is often not reflected on the ways in which data is stored because the. NET Framework handles this issue automatically. However if you want to store the contents of an object in a file, send an object to another process or transmit through the network you need to know the object is represented, because you will probably need to convert it into a different format. This conversion is called serialization.

Serialization, implemented in the System.Runtime.Serialization namespace, is a process that converts an object into a linear sequence of bytes to be stored or transferred. Deserialization is the opposite process or the conversion of a sequence of bytes in a serialized object.

Let us now see the steps to follow to make the serialization of an object:

  1. Create a stream object to store the result of serialization
  2. Create a BinaryFormatter object
  3. Call the BinaryFormatter.Serialize to serialize the object and store the output in the stream created earlier
At the level of code serialization can be done very easily, as shown in the following example





 string data = "Data to be stored in a file."









 / / Create the file in which to store data







 FileStream fs = new FileStream ("C: \ \ Serializzazione.txt", FileMode.Create);









 / / Create a BinaryFormatter object to perform serialization







 BinaryFormatter bf = new BinaryFormatter ();

 







 / / Use the BinaryFormatter object to

 





 / / Serialize the data and insert them in the file







 bf.Serialize (fs, data);

 







 / / Close the file







 fs.Close ();



Launching the application and opening the file with Notepad Serializzazione.txt generated we will see that it contains the string we set in our example, accompanied by binary information (that Notepad will display as symbols) that are useful for subsequent deserialization
Clearly the above example because if it is only demonstration data to process consist of a simple string is more logical to write directly on the same text file. The serialization becomes very useful when information should be treated rather more complex, for which, however, the steps to take are exactly the same as the example shown.

In the same category ...
E-Learning
Ruby and Ruby on Rails (Course) Ruby and Ruby on Rails (Course)
Create software and Web applications with Ruby and RoR. From 39 €.
SQL and Database (Course) SQL and Database (Course)
Create and manage relational databases. From 39 €.
Webmaster Advanced (Course) Webmaster Advanced (Course)
Become a professional Webmaster. From 39 €.
Sponsored Links