The article talks about serialization of objects in XML format . Serialization is a process by which an object's state is transformed in some serial data format, such as XML or binary format. Serialization is the process of converting an object into a form that can be readily transported. For example, you can serialize an object and transport it over the Internet using HTTP between a client and a server. XML serialization results in strongly typed classes with public properties and fields that are converted to a serial format (in this case, XML) for storage or transport.
We can serialize a populated DataSet object to an XML file by using XmlSerializer Class.
We can serialize a populated DataSet object to an XML file by using XmlSerializer Class.
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 by using XmlSerializer Calss. Code is given below :-
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" />
</form>
</body>
</html>
<!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" />
</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)
{
}
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;
//Specify the place wherever you want to store the serialXML.xml file, In my case i am storing the file in the app_Data in root directory;
{
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;
//Specify the place wherever you want to store the serialXML.xml file, In my case i am storing the file in the app_Data in root directory;
serialWriter = new StreamWriter(Server.MapPath("~/App_Data/serialXML.xml"));
XmlSerializer xmlWriter = new XmlSerializer(ds.GetType());
xmlWriter.Serialize(serialWriter, ds);
serialWriter.Close();
ds.Clear();
}
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);
}
}
Result Will be (Out Put of the Program will be ) :-
<?xml version="1.0" encoding="utf-8"?>
<DataSet>
<xs:schema id="NewDataSet" xmlns="" xmlns:xs="http://www.w3.org/2001/XMLSchema" xmlns:msdata="urn:schemas-microsoft-com:xml-msdata">
<xs:element name="NewDataSet" msdata:IsDataSet="true" msdata:UseCurrentLocale="true">
<xs:complexType>
<xs:choice minOccurs="0" maxOccurs="unbounded">
<xs:element name="School">
<xs:complexType>
<xs:sequence>
<xs:element name="Student_Roll" type="xs:int" minOccurs="0" />
<xs:element name="Student_Name" type="xs:string" minOccurs="0" />
<xs:element name="Student_Marks" type="xs:int" minOccurs="0" />
</xs:sequence>
</xs:complexType>
</xs:element>
</xs:choice>
</xs:complexType>
</xs:element>
</xs:schema>
<diffgr:diffgram xmlns:msdata="urn:schemas-microsoft-com:xml-msdata" xmlns:diffgr="urn:schemas-microsoft-com:xml-diffgram-v1">
<NewDataSet>
<School diffgr:id="School1" msdata:rowOrder="0" diffgr:hasChanges="inserted">
<Student_Roll>1</Student_Roll>
<Student_Name>Rakesh Kumar</Student_Name>
<Student_Marks>900</Student_Marks>
</School>
<School diffgr:id="School2" msdata:rowOrder="1" diffgr:hasChanges="inserted">
<Student_Roll>2</Student_Roll>
<Student_Name>Rja</Student_Name>
<Student_Marks>889</Student_Marks>
</School>
<School diffgr:id="School3" msdata:rowOrder="2" diffgr:hasChanges="inserted">
<Student_Roll>3</Student_Roll>
<Student_Name>Krishna</Student_Name>
<Student_Marks>880</Student_Marks>
</School>
<School diffgr:id="School4" msdata:rowOrder="3" diffgr:hasChanges="inserted">
<Student_Roll>4</Student_Roll>
<Student_Name>Sudidhi Das</Student_Name>
<Student_Marks>870</Student_Marks>
</School>
</NewDataSet>
</diffgr:diffgram>
</DataSet>
No comments:
Post a Comment