<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.1//EN" "http://www.w3.org/TR/xhtml11/DTD/xhtml11.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" >
<head runat="server">
<title>Test DataSet Serialization</title>
</head>
<body>
<div id="pageContent">
<form id="form1" runat="server">
<table>
<tr>
<td><asp:Button Text="Serialize as XML" runat="server" ID="XmlButton" OnClick="XmlButton_Click" Width="200px" /></td>
<td><asp:Label ID="XmlSize" runat="server" /></td>
</tr>
<tr>
<td><asp:Button Text="Serialize as Binary" runat="server" ID="BinButton" OnClick="BinButton_Click" Width="200px" /></td>
<td><asp:Label ID="BinSize" runat="server" /></td>
</tr>
</table>
</form>
</div>
</body>
</html>
File: Default.aspx.cs
using System;
using System.Data;
using System.Configuration;
using System.Collections;
using System.Web;
using System.Data.SqlClient;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
public partial class Default : System.Web.UI.Page
{
private string XmlFile = @"c:\serial.xml";
private string BinFile = @"c:\serial.bin";
private DataSet GetData()
{
DataSet ds = new DataSet();
SqlDataAdapter adapter = new SqlDataAdapter(
"SELECT * FROM [order details]",
ConfigurationManager.ConnectionStrings["NorthwindConnectionString"].ConnectionString);
adapter.Fill(ds);
return ds;
}
protected void XmlButton_Click(object sender, EventArgs e)
{
DataSet ds = GetData();
ds.RemotingFormat = SerializationFormat.Xml;
StreamWriter writer = new StreamWriter(XmlFile);
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(writer.BaseStream, ds);
writer.Close();
FileInfo fi = new FileInfo(XmlFile);
XmlSize.Text = (fi.Length/1024).ToString() + " KB";
}
protected void BinButton_Click(object sender, EventArgs e)
{
DataSet ds = GetData();
ds.RemotingFormat = SerializationFormat.Binary;
StreamWriter writer = new StreamWriter(BinFile);
BinaryFormatter bin = new BinaryFormatter();
bin.Serialize(writer.BaseStream, ds);
writer.Close();
FileInfo fi = new FileInfo(BinFile);
BinSize.Text = (fi.Length/1024).ToString() + " KB";
}
}
|