How to Serialize and DeSerialize an object into XML

In this post, I will show how to Serialize and DeSerialize an object (can be a collection object or an object) into XML format using ASP.NET System.Xml.Serialization.XmlSerializer class.

Before I explain how to Serialize and DeSerialize an object, let me tell you why to serialize. We should serialize an object if we want to store the object instance into the disk or we want to store the object into Session or Cache (storing serialized object into Cache or Session works faster than storing the object directly) or pass the object between applications regardless of which platform they have hosted on and technology they are built-in.

Now, why use XML Serialization? As XML is an open standard, XML stream can be processed by any application, as needed, regardless of platform. XML Serialization covers (Serializes) the public fields and properties of an object, or parameters and return values of a method, into an XML stream.

Required namespace to Serialize and DeSerialize an object in this article are:

  1. System.IO
  2. System.Xml
  3. System.Xml.Serialization

To show how to Serialize and DeSerialize an object, I will create a class called XmlClass with two public variables named name, email, and address. My code for this post is as follows

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="XMLSerialize.aspx.cs"  
Inherits="XMLSerialize" %>  

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  
<html xmlns="">  
<head runat="server">  
 <title>Untitled Page</title>  
 <form id="form1" runat="server">  
         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Serialize" />&nbsp;  
         <asp:Button ID="Button2" runat="server" Text="DeSerialize" OnClick="Button2_Click" />&nbsp;  
using System;  
using System.Data;  
using System.Configuration;  
using System.Collections;  
using System.Web;  
using System.Web.Security;  
using System.Web.UI;  
using System.Web.UI.WebControls;  
using System.Web.UI.WebControls.WebParts;  
using System.Web.UI.HtmlControls;  
using System.Xml;  
using System.Xml.Serialization;  
using System.IO;  
public partial class XMLSerialize : System.Web.UI.Page  
    protected void Page_Load(object sender, EventArgs e)  
    protected void Button1_Click(object sender, EventArgs e)  
        XmlClass obj = new XmlClass();  = "test";  
        obj.emial = "";  
        obj.address = "test";  
        string s = obj.SerializeAnObject(obj);  
        Response.ContentType = "text/xml";  
        Response.ContentEncoding = System.Text.Encoding.UTF8;  
    protected void Button2_Click(object sender, EventArgs e)  
public class XmlClass  
    public string name = string.Empty;  
    public string emial = string.Empty;  
    public string address = string.Empty;  
    public string SerializeAnObject(object obj)  
        XmlDocument doc = new XmlDocument();  
        XmlSerializer serializer = new XmlSerializer(obj.GetType());  
        MemoryStream stream = new MemoryStream();  
            serializer.Serialize(stream, obj);  
            stream.Position = 0;  
            return doc.InnerXml;  
    public object DeSerializeAnObject(string xmlOfAnObject)  
        XmlClass myObject = new XmlClass();  
        StringReader read = new StringReader(xmlOfAnObject);  
        XmlSerializer serializer = new XmlSerializer(myObject.GetType());  
        System.Xml.XmlReader reader = new XmlTextReader(read);  
            myObject = (XmlClass)serializer.Deserialize(reader);  
            return myObject;  

Post a Comment

Please do not post any spam link in the comment box😊

Previous Post Next Post

Blog ads