Step by step to Object Serialization

author-image
CIOL Bureau
Updated On
New Update


XML serialization in .NET



Serialization is the process of converting an object or a group of objects of a class into a medium that can be persisted or stored. In .NET, you can serialize an object of a class in the following four ways:

  1. File System

  2. Database Table

  3. SOAP Format

  4. XML file.
  5. The process of recovering a serialized content from any of the above medium is termed as Deserialization. This article will teach you how to serialize and deserialize a class to and from an XML file. But before beginning the discussion, let us look at some of the useful facts regarding the subject matter. An interesting point to note is that when you serialize an object all the values including that of properties are also stored in the appropriate manner.

    Related .NET Framework Classes

















    Class


    Description


    BinaryFormatter


    Serializes/Deserializes objects to a binary format (File, Database)


    SoapFormatter


    Serializes/Deserializes objects to a soap format


    XMLSerializer


    Serializes/Deserializes objects to an XML format

    The final output of XML Serialization is a human friendly XML document containing the object data. Let us look at the real working of XMLSerializer class with the help of an ASP.NET application.

    NOTE







    Only public members of a class can be serialized.

    In the code snippet given below (Listing 1.1), two values are serialized to an XML file named — Users.xml. The serialized data will be the one which you will input using the TextBox controls. The XML file will be created upon successful execution of the application. The required explanations for the source code are given in the form of comment entries.


    Listing 1.1


    Source Code: SerializeUsers.aspx


    Execution URL: http://localhost/SerializeUsers.aspx

    <%@ Page Language="VB" %>


    <%@ Import Namespace = "System.IO"%>


    <%@ Import Namespace = "System.XML"%>


    <%@ Import Namespace = "System.XML.Serialization"%>

    Advertisment

    Upon execution of the above application, the relevant XML file will be created inside the working directory. The code snippet from the generated XML file for the above ASP.NET code is shown in listing 1.2



    Listing 1.2





    Kapil


    35


    From the above code snippet, you can be able to infer the following points

    1. A tag named Users created (Users is the Class Name used in the above ASP.NET application)

    2. Two tags named Name and Age created with the entered data (Name and Age are the name of the properties of the above ASP.NET application)
    3. Deserialization



      You can easily deserialize a class from an XML file using the Deserialize method. First, you have to create an instance of StreamReader class. The relevant XML file should be opened using the OpenText method of File class. Listing 1.3 shows how to deserialize the data stored during the execution of listing 1.1. The appropriate results from the file are displayed on the Label controls.

      Listing 1.3



      Source Code: DeSerializeUsers.aspx


      Execution URL: http://localhost/DeSerializeUsers.aspx



      <%@ Page Language="VB" %>


      <%@ import Namespace="System.IO" %>


      <%@ import Namespace="System.XML" %>


      <%@ import Namespace="System.XML.Serialization" %>


      Get the source codes here:

      DeserializedUsers.aspx

      SerializedUsers.aspx

      Users.xml



      About the author



      Anand Narayanaswamy (Microsoft MVP) works as an independent consultant based in Trivandrum and runs NetAns Technologies (http://www.netans.com), which provides low cost web hosting services. Anand also runs LearnXpress.com (http://www.learnXpress.com) and Dotnetalbum.com (http://www.dotnetalbum.com) and regularly contributes product and book reviews for various websites. He can be reached at ananddotnet@yahoo.co.in


      tech-news