Deserialize a string

C

coder316

Hello,
I have the following method to serialize a dictionay:
string x = SerializePicks(DictionaryOfTeams);
private string SerializePicks(Dictionary<int, string[]>
DictionaryOfTeams)
{
BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
char[] buf = new char[stre.Length];
StreamReader reader = new StreamReader(stre);
stre.Seek(0, SeekOrigin.Begin);
string text;
return text = reader.ReadToEnd();
}

Then I insert it into a Db:
InsertPicks(x, currentUser.ProviderUserKey.ToString(), region);

This works
Now I query the db and get the string representaion on the serialized
Dictionary.
"?????System.Collections.Generic.Dictionary`2[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561........................."

I want to Deserialize this back into my dictionary, but needless to
say I'm stuck.
Any help is greatly appreciated
Thanks
 
J

Jeff Johnson

Then I insert it into a Db:
InsertPicks(x, currentUser.ProviderUserKey.ToString(), region);
This works
Now I query the db and get the string representaion on the serialized
Dictionary.
"?????System.Collections.Generic.Dictionary`2[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561........................."

I can't help you with the rest of the stuff, but I have to ask: what data
type is the column that's holding the serialized dictionary? I've got a
feeling it's varchar, and that's not going to work.
 
P

Peter Duniho

coder316 said:
Hello,
I have the following method to serialize a dictionay:
string x = SerializePicks(DictionaryOfTeams);
private string SerializePicks(Dictionary<int, string[]>
DictionaryOfTeams)
{
BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
char[] buf = new char[stre.Length];
StreamReader reader = new StreamReader(stre);
stre.Seek(0, SeekOrigin.Begin);
string text;
return text = reader.ReadToEnd();
}

The above cannot possibly work. If you generate binary data, you cannot
then interpret that as a text encoding and get something that can then
be reliably turned back into the original binary data.
Then I insert it into a Db:
InsertPicks(x, currentUser.ProviderUserKey.ToString(), region);

This works
Now I query the db and get the string representaion on the serialized
Dictionary.
"?????System.Collections.Generic.Dictionary`2[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561........................."

You need to store something in the database that can be reversed back to
binary. If your database supports storage of binary data, then that's
probably the best solution. Just take the serialized binary data
("stre.ToArray()" will give you a byte[] holding the data you just
wrote) and store it in the database.

If you must store the data in the database, then one option is to use a
different serialization. Specifically, one of the XML-based options,
such as SOAP or just plain XML serialization. Then code like what
you've written above can work, because the data written to your
MemoryStream actually is text data.

Another option is to take the binary data you serialized and convert to
Base64 (see the Convert class). That will give you text, which you can
then store in the database as text.

Each of the above approaches _is_ reversible. Storing binary data,
obviously you just get back binary data which you can then deserialize
by creating a new MemoryStream and deserializing from that. Likewise
XML. If using Base64, then you'll have to convert the Base64 text back
to a byte[] (again, using the Convert class), which you can then wrap in
a MemoryStream and deserialize just as you would have binary data stored
directly in the database.

Pete
 
P

Peter Duniho

Peter said:
[...]
If you must store the data in the database, then one option is to use a
different serialization.

The above should read "If you must store the data as text in the database…"

Sorry for leaving some words out!
 
J

Joe Cool

Hello,
I have the following method to serialize a dictionay:
 string x = SerializePicks(DictionaryOfTeams);
 private string SerializePicks(Dictionary<int, string[]>
DictionaryOfTeams)
    {
        BinaryFormatter formatter = new BinaryFormatter();
        Stream stre = new MemoryStream();
        formatter.Serialize(stre, DictionaryOfTeams);
        char[] buf = new char[stre.Length];
        StreamReader reader = new StreamReader(stre);
        stre.Seek(0, SeekOrigin.Begin);
        string text;
        return  text = reader.ReadToEnd();
    }

Then I insert it into a Db:
InsertPicks(x, currentUser.ProviderUserKey.ToString(), region);

This works
Now I query the db and get the string representaion on the serialized
Dictionary.
" ????   ? System.Collections.Generic.Dictionary`2[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561........................."

I want to Deserialize this back into my dictionary, but needless to
say I'm stuck.
Any help is greatly appreciated
Thanks

We have several applications where I work that stores serialized
class objects as character strings in a Text column in SQL Server. We
serialize like this:
(in case it isn't obvious, the Serialize and Deserialize methods are
contained in the class being processed)

public string Serialize()
{
StringWriter sw = null;
XmlSerializer s = null;
string result;

s = new XmlSerializer(this.GetType());
sw = new StringWriter();
s.Serialize(sw, this);
result = sw.ToString();
sw.Close();

return result;
}

And store the result in the database. Later, after retrieving this
value we deserialize like this:

public void Deserialize(string str)
{
StringReader sr;
MyClassName obj;
XmlSerializer s;

s = new XmlSerializer(this.GetType());
sr = new StringReader(str);
obj = (MyClassName)s.Deserialize(sr);
this.FirstProperty = obj.FirstProperty;
// repeat the above statement for all properties
sr.Close();
}
 
A

Arne Vajhøj

I have the following method to serialize a dictionay:
string x = SerializePicks(DictionaryOfTeams);
private string SerializePicks(Dictionary<int, string[]>
DictionaryOfTeams)
{
BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
char[] buf = new char[stre.Length];
StreamReader reader = new StreamReader(stre);
stre.Seek(0, SeekOrigin.Begin);
string text;
return text = reader.ReadToEnd();
}

Then I insert it into a Db:
InsertPicks(x, currentUser.ProviderUserKey.ToString(), region);

This works
Now I query the db and get the string representaion on the serialized
Dictionary.
"?????System.Collections.Generic.Dictionary`2[[System.Int32,
mscorlib, Version=2.0.0.0, Culture=neutral,
PublicKeyToken=b77a5c561........................."

I want to Deserialize this back into my dictionary, but needless to
say I'm stuck.

Instead of reading the bytes as a string then you should hex encode
or base64 encode them.

Base64:

BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
return Convert.ToBase64String(stre.ToArray());

Hex:

BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
return BitConverter.ToString(stre.ToArray()).Replace("-", "");

And the reverse when deserializing.

You could also just store the binary bytes in the database
by using a column type like VARBINARY and parameters to
insert.

Arne
 

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