Help with Serialization and Deflation

T

TC

I'm trying to write an object to a file using serialization. To
conserve disk space, I'm also trying to compress the data. I've
written what seems like proper code, but I get an error when I try to
read from the file. Specifically, I get the message "There is an error
in XML document (0, 0)." when I call the Deserialize method. Below is
a test procedure which just tries to write and read a string. Can
anyone tell me what is wrong with it?

-TC


Public Shared Sub TestSerialization()

'Create a test object to use for Serialization.
Dim SourceObject As String = "Testing"

'Serialize the object to a test file.
Dim FileStream1 As New System.IO.FileStream("TestFile",
System.IO.FileMode.Create)
Dim DeflateStream1 As New System.IO.Compression.DeflateStream
(FileStream1, System.IO.Compression.CompressionMode.Compress)
Dim XMLSerializer1 As New System.Xml.Serialization.XmlSerializer
(GetType(String))
XMLSerializer1.Serialize(DeflateStream1, SourceObject)
FileStream1.Close()

'Deserialize the object from the file.
Dim FileStream2 As New System.IO.FileStream("TestFile",
System.IO.FileMode.Open)
Dim DeflateStream2 As New System.IO.Compression.DeflateStream
(FileStream2, System.IO.Compression.CompressionMode.Decompress)
Dim XMLSerializer2 As New System.Xml.Serialization.XmlSerializer
(GetType(String))
Dim RecoveredObject2 As String = CType(XMLSerializer2.Deserialize
(DeflateStream2), String)
FileStream2.Close()

'Display the recovered object.
System.Diagnostics.Debug.WriteLine(RecoveredObject2)

End Sub
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top