Search - Articles
DevASP.NET for ASP.NET, VB.NET, XML and C# (C-Sharp) Developers Tuesday, March 03, 2009
Dev Articles
Search Directory
ASP.NET
VB.Net
C-Sharp
SQL Server
 

XML Serialization and Deserialization in ASP.NET

Author: Mike Green
Download Source Code : 1340_XMLSerialization.zip

XmlSerializer class is used for XML Serialization and Deserialization.

 

 

  1. Open MS Visual Studio 2010
  2. File > New > Website > Visual C# or Visual Basic > ASP.NET Empty Web Site
  3. Select Web Location as File System and Click OK
  4. From Menu, Website > Add New Item > Select Web Form and Click Add. Name of the Web Form will be Default.aspx
  5. Drag and Drop a Button control and three Label Controls or write code below in Default.aspx page

    <asp:Button ID="Button1" runat="server" Text="XML Serialize and Deserialize"
                onclick="Button1_Click" />
    <br />
    <asp:Label ID="Label1" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="Label2" runat="server" ></asp:Label>
    <br />
    <asp:Label ID="Label3" runat="server" ></asp:Label>

     
  6. From Menu, Website > Add New Item > Select XML File and Click Add. Name of the XML File will be XMLFile.xml
  7. From Menu, Website > Add New Item > Select Class and Click Add. Rename Class as Employee.cs or Employee.vb
  8. Open Employee class and add following namespaces in it

    C#
     
    using System.IO;
    using System.Xml.Serialization;
     
    VB.NET

    Imports System.IO
    Imports System.Xml.Serialization
     
  9. Write code below in Employee class

    C#
     
    public class Employee
    {
        private int _empCode;
        private string _empName;
        private int _empAge;
     
        public int EmpCode
        {
            get
            {
                return _empCode;
            }
            set
            {
                _empCode = value;
            }
        }
     
        public string EmpName
        {
            get
            {
                return _empName;
            }
            set
            {
                _empName = value;
            }
        }
     
        public int EmpAge
        {
            get
            {
                return _empAge;
            }
            set
            {
                _empAge = value;
            }
        }
        public static void Serialize()
        {
            Employee emp = new Employee();
            emp.EmpCode = 001;
            emp.EmpName = "Jason";
            emp.EmpAge = 37;
     
            XmlSerializer xmlserialize = new XmlSerializer(emp.GetType());
            StreamWriter writer = new StreamWriter(HttpContext.Current.Server.MapPath("~/XMLFile.xml"));
            xmlserialize.Serialize(writer, emp);
            writer.Close();
     
        }
        public static Employee Deserialize()
        {
            Employee emp = new Employee();
            XmlSerializer serializer = new XmlSerializer(emp.GetType());
            StreamReader reader = new StreamReader(HttpContext.Current.Server.MapPath("~/XMLFile.xml"));
     
            emp = (Employee)serializer.Deserialize(reader);
     
            return emp;
     
        }
          
    }
     
    VB.NET
     
    Public Class Employee
        Private _empCode As Integer
        Private _empName As String
        Private _empAge As Integer
     
        Public Property EmpCode() As Integer
            Get
                Return _empCode
            End Get
            Set(ByVal value As Integer)
                _empCode = value
            End Set
        End Property
     
        Public Property EmpName() As String
            Get
                Return _empName
            End Get
            Set(ByVal value As String)
                _empName = value
            End Set
        End Property
     
        Public Property EmpAge() As Integer
            Get
                Return _empAge
            End Get
            Set(ByVal value As Integer)
                _empAge = value
            End Set
        End Property
        Public Shared Sub Serialize()
            Dim emp As New Employee()
            emp.EmpCode = 1
            emp.EmpName = "Jason"
            emp.EmpAge = 37
     
            Dim xmlserialize As New XmlSerializer(emp.GetType())
            Dim writer As New StreamWriter(HttpContext.Current.Server.MapPath("~/XMLFile.xml"))
            xmlserialize.Serialize(writer, emp)
            writer.Close()
     
        End Sub
        Public Shared Function Deserialize() As Employee
            Dim emp As New Employee()
            Dim serializer As New XmlSerializer(emp.GetType())
            Dim reader As New StreamReader(HttpContext.Current.Server.MapPath("~/XMLFile.xml"))
     
            emp = DirectCast(serializer.Deserialize(reader), Employee)
     
            Return emp
     
        End Function
    End Class
     
    Here we have declared private variables and public properties for Employee class. We have written two public Shared or static methods Serialize() and Deserialize() for Serialization and Deserialization. You need to create an object for Employee class to serialize and deserialize it. XmlSerializer class is used to serialize and deserialize object for XML Serialization and deserialization. We have created a StreamWrite object for writing data to XML file and StreamReader object for reading data from XML file. You have to call Serialize() method to Serialize and Deserialize() method to deserialize object.
     
  10. Open code behind file of Default.aspx.cs or Default.aspx.vb and write below code in Button click event.

    C#
     
    protected void Button1_Click(object sender, EventArgs e)
    {
        Employee.Serialize();
     
        Employee emp = Employee.Deserialize();
     
        Label1.Text = emp.EmpCode.ToString();
        Label2.Text = emp.EmpName;
        Label3.Text = emp.EmpAge.ToString();
    }
     
    VB.NET
     
    Protected Sub Button1_Click(ByVal sender As Object, ByVal e As EventArgs) Handles Button1.Click
     
        Employee.Serialize()
     
        Dim emp As Employee = Employee.Deserialize()
     
        Label1.Text = emp.EmpCode.ToString()
        Label2.Text = emp.EmpName
        Label3.Text = emp.EmpAge.ToString()
    End Sub
     
     
    Here we have called our Serialize() method of Employee class to Serialize object and Deserialize() method of Employee class to Deserialize object.
     
  11. Browse the website and click on button to see XML Serialization and Deserialization the result. See XMLFile.xml has the Employee data.  

 

   
Add Article Comment:
Name :
Email Address :
   
Comments :
 
   
<< What is serialization and different types of serialization in ASP.NET

Disclaimer - Privacy
© 2002-2017 DevASP.net