.NET
  Home arrow .NET arrow Page 2 - Binary and XML Serialization
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

Binary and XML Serialization
By: Ayad Boudiab
  • Search For More Articles!
  • Disclaimer
  • Author Terms
  • Rating: 4 stars4 stars4 stars4 stars4 stars / 4
    2009-02-10

    Table of Contents:
  • Binary and XML Serialization
  • Binary Serialization
  • Binary Serialization continued
  • XML Serialization

  • 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


    Binary and XML Serialization - Binary Serialization


    (Page 2 of 4 )

    With binary serialization, the public and private fields of the object, in addition to the class name and the assembly, are converted to a stream of bytes. The process of deserialization will rebuild the original object from the stream. Binary serialization is very efficient, but it is limited to the .NET framework. By that I mean I cannot use a Java application to deserialize an object serialized by a .NET-compatible language. With XML serialization, on the other hand, it is doable since the object is serialized as an XML file, which other languages (not just .NET) will be able to read.

    The first step in serializing an object is to use the[Serializable]attribute. This attribute is placed above the class declaration. When designing applications, it is very important to decide which classes will be serializable as early in the development process as possible, since making the change later will be more complicated. When in doubt, always mark your class as serializable. Let's start with the[Serializable]attribute. To declare a class serializable, do the following:

    [Serializable]

    class Book

    {

    private string isbn;

    public string ISBN

    {

    get { return isbn; }

    set { isbn = value; }

    }

    private string title;

    public string Title

    {

    get { return title; }

    set { title = value; }

    }

    private string author;

    public string Author

    {

    get { return author; }

    set { author = value; }

    }


    public Book()

    {

    isbn = "";

    title = "";

    author = "";

    }


    public Book(string isbn, string title, string author)

    {

    this.isbn = isbn;

    this.title = title;

    this.author = author;

    }


    public override string ToString()

    {

    return string.Format("ISBN: {0}, Title: {1}, Author: {2}",

    isbn, title, author);

    }

    }

    The book class contains the usual declaration with the isbn, title, and author fields. The corresponding properties provide public access to the private fields. We added two constructors and overrode theToString()method. The only addition here is the[Serializable]attribute added to the class declaration. This attribute is signaling to the .NET Framework that objects of this class could be written to a storage medium.

    Please note that the serializable attribute is not inherited. So, if class B inherits from class A, and A is [Serializable], B will NOT automatically be [Serializable]. You need to add the[Serializable]attribute to B as well.

    Now that the class is serializable, let's store an instance of this class in a file. To do so, we use theBinaryFormatterclass:

    IFormatter formatter = new BinaryFormatter();

    Since the instance of the class is stored in a file, it is clear that we need a stream:

    Stream stream = new FileStream("book.bin", FileMode.Create,

    FileAccess.Write, FileShare.None);

    This declaration specifies that we are creating aFileStreamfor writing, and the file name is book.bin. The last step is to call theSerialize()method of theBinaryFormatter to write the object to the file:

    formatter.Serialize(stream, favoriteBook);

    We can place those calls in a handy method that we can call any time we need to serialize a book object:

    private static void SerializeBook(Book favoriteBook)

    {

    IFormatter formatter = new BinaryFormatter();

    Stream stream = new FileStream("book.bin", FileMode.Create,

    FileAccess.Write, FileShare.None);

    formatter.Serialize(stream, favoriteBook);

    stream.Close();

    }

    More .NET Articles
    More By Ayad Boudiab


     

    .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 2 Hosted by Hostway
    For more Enterprise Application Development news, visit eWeek