.NET
  Home arrow .NET arrow Serialization with .NET
ASP Free Forums 
.NET  
ASP  
ASP Code  
ASP.NET  
ASP.NET Code  
BrainDump  
C#  
Code Examples  
Database  
Database Code  
IIS  
Microsoft Access  
MS SQL Server  
Silverlight  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
Mobile Linux 
App Generation ROI 
IBM® developerWorks 
ASP Web Hosting  
ASP.NET Web Hosting 
Windows Web Hosting
 
Weekly Newsletter
 
Developer Updates  
Free Website Content 
 RSS  Articles
 RSS  Forums
 RSS  All Feeds
Write For Us Get Paid 
Request Media Kit
Contact Us 
Site Map 
Privacy Policy 
Support 
 USERNAME
 
 PASSWORD
 
 
  >>> SIGN UP!  
  Lost Password? 
.NET

Serialization with .NET
By: Peyton McCullough
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 5 stars5 stars5 stars5 stars5 stars / 7
    2007-08-14

    Table of Contents:
  • Serialization with .NET
  • Serializing Custom Types
  • Serializing Only Some Internal Data
  • Other Attributes

  • Rate this Article: Poor Best 
      ADD THIS ARTICLE TO:
      Del.ici.ous Digg
      Blink Simpy
      Google Spurl
      Y! MyWeb Furl
    Email Me Similar Content When Posted
    Add Developer Shed Article Feed To Your Site
    Email Article To Friend
    Print Version Of Article
    PDF Version Of Article
     
     
    ADVERTISEMENT


    Serialization with .NET


    (Page 1 of 4 )

    Initializing and manipulating objects in a program is fairly straightforward, but in some situations, this isn't enough. Rather than just being used and discarded, it's sometimes necessary for an object to be preserved for later use. It's also sometimes necessary for objects to be sent to another computer. This process of preserving objects is known as serialization.

    When an object is serialized, its state is stored in such a way that the object can be easily remade at a later time or on a different machine. .NET provides several methods of serializing (and deserializing) objects, as well as several formats in which the object can be saved. This article will explore the serialization functionality provided by .NET.

    Simple Serialization

    To get started, let's serialize a simple object: a string containing a short message. We'll store the string on disk and then restore it by reading the file. Here's the required code, which we'll examine in detail:

    using System;

    using System.IO;

    using System.Runtime.Serialization.Formatters.Binary;

     

    class StringSerialization

    {

       public static void Main(string[] args)

       {

           // The string we want to serialize

           string message = "This is a message.";

           Console.WriteLine(message);

           

           // Serialization takes place here

           FileStream myStream = File.Create("message");

           BinaryFormatter formatter = new BinaryFormatter();

           formatter.Serialize(myStream, message);

           

           // Clear our string

           message = null;

           

           // Restore the contents of our string -- deserialization

           myStream.Position = 0;

           message = (string)formatter.Deserialize(myStream);

           myStream.Close();

           Console.WriteLine(message);

       }

    }

    The above code saves the message in a file named message. It then clears the contents of the message (just for fun) and then restores the saved object.

    As you can see, .NET's mechanisms for serialization are located in the System.Runtime.Serialization namespace. Here, we make use of the namespace System.Runtime.Serialization.Formatters.Binary. Formatters contains, as its name implies, different formatters for serialization. The formatter is in charge of exactly how the object to be serialized is represented. Here, we use BinaryFormatter. You can see how BinaryFormatter formats our string by examining the contents of the message file, which look something like this:

    ����This is a message.

    Since we're only serializing a string, the file remains somewhat readable, with the exception of a number of special characters. However, with more complicated objects, the output produced by BinaryFormatter is quite unreadable. In many situations, it doesn't matter what the serialized object looks like; you may not care. However, in other situations, the serialized object may need to be more human-readable. This is a downside to the BinaryFormatter, but, thankfully, this isn't the only way to represent serialized data in .NET.

    After message is initialized, a FileStream is created, which provides read/write access:

    FileStream myStream = File.Create("message");

    Note, however, that any type of Stream will do. Next, we create a BinaryFormatter, whose function was already explained:

    BinaryFormatter formatter = new BinaryFormatter();

    The object is then serialized simply by calling the Serialize method of our BinaryFormatter object:

    formatter.Serialize(myStream, message);

    Next, our position in myStream is reset to the beginning of the file so that we can begin reading. The process of deserialization is no less simple than the process of serialization. The Deserialize method is called, and myStream is passed:

    message = (string)formatter.Deserialize(myStream);

    Deserialize returns the object contained within the Stream, which must be cast to the proper type -- here, it is string. Finally, we clean up by closing myStream.

    Not hard, is it?

    More .NET Articles
    More By Peyton McCullough


       · Hello.This article introduces some of the basics of serialization, covering...
       · Ease to understand and definitely good article
       · Thank you, that was very helpful and easy to follow.
     

    .NET ARTICLES

    - Introducing .NET
    - Building Business Objects for an Application
    - Methods for an Application`s Business Logic ...
    - Properties of an Application`s Business Logi...
    - Classes and Properties in an Application`s B...
    - Organizing Code for the Business Logic Layer...
    - Building the Business Logic Layer
    - Completing a Business Layer with Windows Wor...
    - Building Applications with Windows Workflow ...
    - Building the Data and Business Layers Using ...
    - The Transformed XML Explorer in MFC
    - List Control and Property Grid with the MFC ...
    - Font, Shell and Masked Edit Controls for MFC
    - Color, Link and Image Editor Controls for M...
    - New Controls for MFC





    © 2003-2010 by Developer Shed. All rights reserved. DS Cluster 6 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek