Today to do a project used to serialize dataset objects into a file, and upload to the server, but the resulting file is relatively large, think of compression, looked for information, with GZipStream to compress a bit, the effect is very ideal. Now sort this part of the code:
Using System.Data;
Using System.IO;
Using System.IO.Compression;
Using System.Runtime.Serialization;
Using System.Runtime.Serialization.Formatters.Binary;
Namespace Datasetserializerdeserialize
{
Class Program
{
<summary>
Serializes the DataSet object and compresses
</summary>
<param name= "DS" ></param>
static void Datasetserializercompression (DataSet ds)
{
IFormatter formatter = new BinaryFormatter ();//define BinaryFormatter to serialize the DataSet object
MemoryStream ms = new MemoryStream ();//create memory Stream object
Formatter. Serialize (MS, DS);//serialization of DataSet objects to memory stream
byte[] buffer = Ms. ToArray ()//writes a memory stream object to a byte array
Ms. Close ();//Shut down memory stream object
Ms. Dispose ();/release Resources
FileStream fs = File.create ("DatasetCompression.dat");//Create File
GZipStream GZipStream = new GZipStream (FS, compressionmode.compress, True);//Create a compressed object
Gzipstream.write (buffer, 0, buffer. LENGTH);//write compressed data to file
Gzipstream.close ();//Turn off the compression stream, here to note: Be sure to close, otherwise the decompression will appear less than 4K file read data, more than 4K of the file read incomplete
Gzipstream.dispose ()//Release Object
Fs. Close ();//Off stream
Fs. Dispose ();/Release Object
}
<summary>
Do not compress a direct serialized dataset
</summary>
<param name= "DS" ></param>
static void Datasetserializer (DataSet ds)
{
IFormatter formatter = new BinaryFormatter ();//define BinaryFormatter to serialize the DataSet object
FileStream fs = File.create ("Dataset.dat");//Create File
Formatter. Serialize (FS, DS);//serialization of DataSet objects to a file
Fs. Close ();//Off stream
Fs. Dispose ();/Release Object
}
static void Main (string[] args)
{
DataTable table = new DataTable ("ParentTable");
DataColumn column;
DataRow Row;
column = new DataColumn ();
Column. DataType = System.Type.GetType ("System.Int32");
Column. ColumnName = "id";
Column. ReadOnly = true;
Column. Unique = true;
Table. Columns.Add (column);
column = new DataColumn ();
Column. DataType = System.Type.GetType ("System.String");
Column. ColumnName = "ParentItem";
Column. AutoIncrement = false;
Column. Caption = "ParentItem";
Column. ReadOnly = false;
Column. Unique = false;
Table. Columns.Add (column);
datacolumn[] Primarykeycolumns = new Datacolumn[1];
Primarykeycolumns[0] = table. columns["id"];
Table. PrimaryKey = Primarykeycolumns;
DataSet DataSet = new DataSet ();
DATASET.TABLES.ADD (table);
for (int i = 0; I <= i++)
{
row = table. NewRow ();
row["id"] = i;
row["ParentItem"] = "parentitem" + i;
Table. Rows.Add (row);
}
Datasetserializer (DataSet);
Datasetserializercompression (DataSet);
}
}
}
Here we will post the serialized and compressed parts of the code, and post the uncompressed and deserialized code tomorrow.