Friday 31 January 2014

How to Deserialize a XML file into .Net Object

The article talks about Deserialization of an XML file back to an object. Deserialization is used to convert the byte of data, such as XML or binary data, to object type. Deserialization reconstructs the object from the stream.

For showing the example we create a page on asp.net and create the data table by the code and add this table in the dataset after that serialize that DataSet object to an XMl file stored in app_Data from the root directory by using XmlSerializer Calss. After that we Deserialization the XML file which is store in app_data folder and convert that file into the .net object (Dataset Object) and populate the gridview. The code is given bleow :- 

HTML Page :- Default2.aspx

<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default2.aspx.cs" Inherits="Default2" %>
<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml">
<head id="Head1" runat="server">
    <title>Reading an XML File</title>
    <style type="text/css">
body { font-family: Verdana; font-size: 9pt; }
.name { background-color: #F7F7F7; }
    </style>
</head>
<body>
    <form id="form1" runat="server">
    <div>
</div>
        <asp:Button ID="serialize" runat="server" OnClick="serialize_Click" Text="serialize" />
        <asp:Button ID="Deserialize" runat="server" OnClick="Deserialize_Click" Text="Deserialize" />
        <asp:GridView ID="GridView1" runat="server">
        </asp:GridView>
    </form>
</body>
</html>


Code Behind Page :- Default2.cs

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Data;
using System.Xml.Serialization;
using System.IO;

public partial class Default2 : System.Web.UI.Page

{
    DataTable dt;
    protected void Page_Load(object sender, EventArgs e)
    {
        
    }
// Serialization of the data 
    protected void serialize_Click(object sender, EventArgs e)
    {
        DataSet ds = new DataSet();
        dt = new DataTable();
        dt.Columns.Add(new DataColumn("Student_Roll", Type.GetType("System.Int32")));
        dt.Columns.Add(new DataColumn("Student_Name", Type.GetType("System.String")));
        dt.Columns.Add(new DataColumn("Student_Marks", Type.GetType("System.Int32")));
        fillRows(1, "Rakesh Kumar", 900);
        fillRows(2, "Rja", 889);
        fillRows(3, "Krishna", 880);
        fillRows(4, "Sudidhi Das",870);
        ds.Tables.Add(dt);
        ds.Tables[0].TableName = "School";

        StreamWriter serialWriter;
        serialWriter = new StreamWriter(Server.MapPath("~/App_Data/serialXML.xml"));
        XmlSerializer xmlWriter = new XmlSerializer(ds.GetType());
        xmlWriter.Serialize(serialWriter, ds);
        serialWriter.Close();
        ds.Clear();
    }

    private void fillRows(int Roll, string Name, int Marks)
    {
        DataRow dr;
        dr = dt.NewRow();
        dr["Student_Roll"] = Roll;
        dr["Student_Name"] = Name;
        dr["Student_Marks"] = Marks;
        dt.Rows.Add(dr);
    }
//Deserialize XML File
    protected void Deserialize_Click(object sender, EventArgs e)
    {
            DataSet ds = new DataSet();
            XmlSerializer xmlSerializer = new XmlSerializer(typeof(DataSet));
  FileStream readStream =new FileStream(Server.MapPath("~/App_Data/serialXML.xml"), FileMode.Open);
            ds = (DataSet)xmlSerializer.Deserialize(readStream);
            readStream.Close();
            GridView1.DataSource = ds.Tables[0];
            GridView1.DataBind();
       
    }

}

Result will be  :- 




Student_Roll Student_Name Student_Marks
1 Rakesh Kumar 900
2 Rja 889
3 Krishna 880
4 Sudidhi Das 870

No comments:

Post a Comment