From: TC on
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
From: dotNetDave on
It looks like you are trying to serialize the deflate stream. I would
serialize the xml first, then compress it.

Here is a link to some great serialization code that I wrote:
http://dotnettips.com/2009/04/13/SerializeAndDeserializeXmlToDiskUsingGenerics.aspx

David

======================================
David McCarter [Microsoft MVP]
www.dotNetTips.com
David McCarter''''s .NET Coding Standards available at:
http://codingstandards.notlong.com


"TC" wrote:

> 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
> .
>