<%@ Page Language="C#" AutoEventWireup="true" CodeFile="Default.aspx.cs"
Inherits="Default"%>
Test DataSet Serialization
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";
}
}