Re: Serializing a Hashtable



Mio,

A few things.

The first is that you don't really have to initialize the memory stream
with your array when serializing the instance, since it will allocate space
as needed when the serializer writes to it.

The other thing about this is that when you pass the byte array to the
constructor, it makes the memory stream fixed-size. So if you need more
than one kilobyte for the serialized instance, it will throw an exception.

Additionally, when you are returning the byte array, it contains extra
bytes that are not part of the serialized instance (in the case where you
need less than 1K).

Your get code should look like this:

// Create the formatter.
BinaryFormatter formatter = new BinaryFormatter();

// Create the stream.
using (MemoryStream memoryStream = new MemoryStream())
{
// No need to set the position, since it is already at 0.
// Serialize the instance.
formatter.Serialize(memoryStream, Directions);

// Return the array. This will be properly sized.
return memoryStream.ToArray();
}

Now, when dealing with deserializing the instance, your code should work
just fine. However, it could stand to be cleaned up, like so:

// Pass the value into the memory stream, since we are just reading.
using (MemoryStream memoryStream = new MemoryStream(value))
{
// Create the formatter.
BinaryFormatter bf = new BinaryFormatter();

// Set the hashtable.
Directions = (Hashtable) bf.Deserialize(memoryStream);
}

Hope this helps.


--
- Nicholas Paldino [.NET/C# MVP]
- mvp@xxxxxxxxxxxxxxxxxxxxxxxxxxx


<MioTheGreat@xxxxxxxxx> wrote in message
news:1131449075.225070.42990@xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx
I know how to take a single hashtable, and then use a binaryformatter
and a filestream to dump it to a file, but I need to serialize and
deserialize a hashtable inside a class. I've been trying this:

[XmlIgnore]
public Hashtable Directions;

public byte[] DirectionsSerialized
{
get
{
byte[] bData = new byte[1024];
BinaryFormatter formatter = new BinaryFormatter();
MemoryStream memoryStream = new MemoryStream(bData);
memoryStream.Position = 0;
formatter.Serialize(memoryStream, Directions);

return bData;
}
set
{
MemoryStream memoryStream = new MemoryStream();
memoryStream.Write(value, 0, value.Length);
BinaryFormatter bf = new BinaryFormatter();
memoryStream.Position = 0;
Directions = (Hashtable)bf.Deserialize(memoryStream);
}
}



Which appears to serialize fine, but upon deserialization, it throws an
error:

"'', hexadecimal value 0x1B, is an invalid character. Line 299,
position 18."

Thanks guys.


.



Relevant Pages

  • Re: Saving a Bitmap or Image type to a Byte() in a database - SQL Server 2005
    ... Dim memoryStream As New MemoryStream ... Byte, ie a Byte array. ... Create a memory stream from the byte array and then create the image from ...
    (microsoft.public.dotnet.framework.drawing)
  • Re: Wokring With System.Net.Sockets
    ... > serialize to a byte array (via a memory stream) and send the raw bytes ... > or you can serialize to an XML string and send that. ...
    (microsoft.public.dotnet.languages.vb)
  • Re: Zip file using a stream
    ... I am focused at figuring out why the zip file is corrupt in cases #1 ... // This method outputs a zip file to a memorystream ... // The input in all three test cases is a byte array from different ... write additional junk to the ZipOutputStream at the end might corrupt ...
    (microsoft.public.dotnet.languages.csharp)
  • RE: Craete tiff Image Page by Page
    ... the tiff image from a dll other than "Memory buffer" ... To load a Tiff into a memoryStream, ... use a Filestream to read it into a byte array, ... EncoderParameter(Encoder.Compression, imgCompression); ...
    (microsoft.public.dotnet.languages.csharp)
  • Problem with BinaryFormatter Deserialize method
    ... public byteSerialize() ... MemoryStream memoryStream = null; ... BinaryFormatter formatter = null; ...
    (microsoft.public.dotnet.languages.csharp)

Loading