.NET
  Home arrow .NET arrow Serialization with .NET
Iron Speed
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  
Visual Basic.NET  
Windows Scripting  
Windows Security  
XML  
ASP Web Hosting  
ASP.NET Web Hosting 
Dedicated Servers 
Download TestComplete 
Windows Web Hosting
 
IBM® developerWorks 
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: 4 stars4 stars4 stars4 stars4 stars / 6
    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
     
     
    Iron Speed
     
    ADVERTISEMENT

    Ajax Application Generator Generate database and reporting .NET Web apps in minutes. Quickly create visually stunning, feature-rich apps that are easy to customize and ready to deploy. Download Now!

    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
     

    .NET ARTICLES

    - Knowing Your Environment: the System.Environ...
    - Creating the Home Page for a Simple Storefro...
    - LINQ Quickly with Language Integrated Queries
    - Introducing LINQ to SQL Designer using Visua...
    - Beginning LINQ to SQL Using Visual Studio 20...
    - Coding an AjaxPro.NET Based Search Engine fo...
    - Building an AjaxPro.NET Based Search Engine ...
    - Delving Deeper into Serialization with .NET
    - Serialization with .NET
    - Understanding Interface-Based Programming
    - Generics and Interface-Based Programming
    - Delving Deeper into Interface-Based Programm...
    - Interface-Based Programming
    - The Why and How of the SplitContainer Control
    - Building an ASP.NET 2.0 Master Page in Three...




    © 2003-2008 by Developer Shed. All rights reserved. DS Cluster 1 hosted by Hostway