Using Serialization To Measure .NET DataSet Size
- Posted by admin on April 9th, 2008 filed in Uncategorized
In my previous post, I did commented that serialization could do the trick to measure the size of a DataSet object.
I am using both BinaryFormatter and SoapFormatter this time, the code is as follows:
using System; using System.Collections.Generic; using System.Data; using System.IO; using System.Text; using System.Runtime.Serialization; using System.Runtime.Serialization.Formatters.Soap; using System.Runtime.Serialization.Formatters.Binary; namespace ConsoleApplication1 { class Program { public static int MeasureDataSetByXML(DataSet ds) { String firstXML = ds.GetXml(); UnicodeEncoding uniEnc = new UnicodeEncoding(); return uniEnc.GetByteCount(firstXML); } public static long MeasureDataSetByBinaryFormatter(DataSet ds) { return MeasureDataSetSerialization(ds, new BinaryFormatter()); } public static long MeasureDataSetBySoapFormatter(DataSet ds) { return MeasureDataSetSerialization(ds, new SoapFormatter()); } public static long MeasureDataSetSerialization(DataSet ds, IFormatter formatter) { FileInfo firstFile = new FileInfo(”Test.txt”); if (firstFile.Exists) firstFile.Delete(); FileStream firstStream = firstFile.OpenWrite(); //Try Serialize try { formatter.Serialize(firstStream, ds); firstStream.Flush(); firstStream.Close(); } catch (Exception ex) { Console.WriteLine(”Failed to Serialized. Reason: ” + ex.Message); } //Refresh the FileInfo firstFile.Refresh(); return firstFile.Length; } static void Main(string[] args) { //Prepare the Data DataSet ds = new DataSet(); Random r = new Random(); DataTable dt = new DataTable(”TestTable”); for (int i = 1; i
sodeve.net