views:

60

answers:

2

Is is possible to convert a byte array back to a List<List<Point>> ?

LE: I am saving the List<List<Point>> in a database BLOB field. When I retrieve it, I want to convert it back to a List<List<Point>>.

The data is kept into an SQLite database and gets set with:

...
cmd.Parameters.AddWithValue("@bynaryData", theList);
...

So I have the byte[], but I cannot figure out how to convert it. How should the de/serialization look like ?

+1  A: 

If the byte array was originally a List<List<Point>> and then properly serialized, then yes.

With a little more detail and example code, we'd be able to give you a more exact answer as to how though.

Justin Niessner
A: 

First serialize the object to a MemoryStream with BinaryFormatter.Serialize(). MemoryStream.GetBytes() now gets you a byte[] that you can write to the blob. Reading is requires deserializing the byte[] you got from the blob, now using BinaryFormatter.Deserialize(). Cast to a List<List<Point>>.

XmlSerializer will work too, the blob is just bigger. But it does let you store the serialized value to an nvarchar(max) column in the dbase table instead of having to use a blob. It is safer since it won't depend on the .NET framework version, important if this data is going to exist for a while. Serialize to a StringStream to get the string value of the generated XML.

Last but not least, consider adding a table to your dbase that can store Points. It is the clean solution.

Hans Passant