C # extract and deserialize dataset objects

Source: Internet
Author: User

Yesterday we talked about dataset serialization and compression. Today we extract and deserializeCodeWrite down:

 

///   <Summary>
/// Deserialization and compression of Dataset
///   </Summary>
///   <Param name = "_ filepath"> </param>
///   <Returns> </returns>
Static Dataset datasetdeserializedecompress ( String _ Filepath)
{
Filestream FS = File. openread (_ filepath ); // Open a file

FS. Position =   0 ; // Set the file stream location

Gzipstream =   New Gzipstream (FS, compressionmode. Decompress ); // Create extract object

Byte [] Buffer =   New   Byte [ 4096 ]; // Define data buffer

Int Offset =   0 ; // Define the read location

Memorystream MS =   New Memorystream (); // Define memory stream

While (Offset = Gzipstream. Read (buffer, 0 , Buffer. Length )) ! =   0 )
{
Ms. Write (buffer, 0 , Offset ); // Decompressed data is written to the memory stream
}

Binaryformatter sfformatter= NewBinaryformatter ();//Define binaryformatter to deserialize the DataSet object

Ms. Position= 0;//Set the memory stream location

Dataset Ds;

Try
{
DS = (Dataset) sfformatter. deserialize (MS ); // Deserialization
}
Catch
{
Throw ;
}
Finally
{
Ms. Close (); // Disable memory stream
Ms. Dispose (); // Release resources
}
FS. Close (); // Close file stream
FS. Dispose (); // Release resources
Gzipstream. Close (); // Close extract stream
Gzipstream. Dispose (); // Release resources
Return DS;
}

///   <Summary>
/// Deserialization of uncompressed Dataset
///   </Summary>
///   <Param name = "_ filepath"> </param>
///   <Returns> </returns>
Static Dataset datasetdeserialize ( String _ Filepath)
{
Filestream FS = File. openread (_ filepath ); // Open a file

FS. Position =   0 ; // Set the file stream location

Binaryformatter sfformatter =   New Binaryformatter (); // Define binaryformatter to deserialize the DataSet object

Dataset Ds;

Try
{
DS = (Dataset) sfformatter. deserialize (FS ); // Deserialization
}
Catch
{
Throw ;
}
Finally
{
FS. Close (); // Disable memory stream
FS. Dispose (); // Release resources
}
FS. Close (); // Close file stream
FS. Dispose (); // Release resources
Return DS;
}

 

 

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.