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"  
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">  
<html xmlns="http://www.w3.org/1999/xhtml">  
<head runat="server">  
 <title>Untitled Page</title>  
</head>  
<body>  
 <form id="form1" runat="server">  
     <div>  
         <asp:Button ID="Button1" runat="server" OnClick="Button1_Click" Text="Serialize" />&nbsp;  
         <asp:Button ID="Button2" runat="server" Text="DeSerialize" OnClick="Button2_Click" />&nbsp;  
     </div>  
 </form>  
</body>  
</html>  
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();  
        obj.name = "test";  
        obj.emial = "test@gmail.com";  
        obj.address = "test";  
        string s = obj.SerializeAnObject(obj);  
        Response.ContentType = "text/xml";  
        Response.ContentEncoding = System.Text.Encoding.UTF8;  
        Response.Write(s);  
        Response.End();  
  
  
    }  
    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();  
        try  
        {  
            serializer.Serialize(stream, obj);  
            stream.Position = 0;  
            doc.Load(stream);  
            return doc.InnerXml;  
        }  
        catch  
        {  
            throw;  
        }  
  
        finally  
        {  
            stream.Close();  
            stream.Dispose();  
        }  
    }  
    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);  
        try  
        {  
            myObject = (XmlClass)serializer.Deserialize(reader);  
            return myObject;  
        }  
        catch  
        {  
            throw;  
        }  
        finally  
        {  
            reader.Close();  
            read.Close();  
            read.Dispose();  
        }  
  
    }  
}

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

إرسال تعليق (0)
أحدث أقدم