Serialize a Dictionary

  • Thread starter Thread starter AMP
  • Start date Start date
A

AMP

Hello,
I am trying to serialize a Generic Dictionary(of Arrays) to store in a
Db and I am a little stuck.
I want to create a stream to write to and serialize the dictionary,
BUT I dont want to create a file.
I am a little unsure of how.
Any help would be appreciated.
Thanks
 
AMP said:
Hello,
I am trying to serialize a Generic Dictionary(of Arrays) to store in a
Db and I am a little stuck.
I want to create a stream to write to and serialize the dictionary,
BUT I dont want to create a file.
I am a little unsure of how.
Any help would be appreciated.

Stream stream = new MemoryStream();
 
   Stream stream = new MemoryStream();

Peter,
I probobly need a little more. Unfortunatly C# doesnt have the
Serialize functions that php has.
This is what I have:
string Round1 = "A,B,C,D,E,F,G,H";
string Round2 = "A,B,C,D";
string Round3 = "A,B";
string Round4 = "A";

string[] Round1parts = Round1.Split(',');
string[] Round2parts = Round2.Split(',');
string[] Round3parts = Round3.Split(',');
string[] Round4parts = Round4.Split(',');

Dictionary<int, string[]> DictionaryOfTeams = new
Dictionary<int, string[]>();

DictionaryOfTeams.Add(1, Round1parts);
DictionaryOfTeams.Add(2, Round2parts);
DictionaryOfTeams.Add(3, Round3parts);
DictionaryOfTeams.Add(4, Round4parts);


Now I would like to serialize this object(DictionaryOfTeams) and store
it in the Db. (And later unwind it)But I am lost.
Thanks
 
I probobly need a little more. Unfortunatly C# doesnt have the
Serialize functions that php has.
This is what I have:
string Round1 = "A,B,C,D,E,F,G,H";
string Round2 = "A,B,C,D";
string Round3 = "A,B";
string Round4 = "A";

string[] Round1parts = Round1.Split(',');
string[] Round2parts = Round2.Split(',');
string[] Round3parts = Round3.Split(',');
string[] Round4parts = Round4.Split(',');

Dictionary<int, string[]> DictionaryOfTeams = new
Dictionary<int, string[]>();

DictionaryOfTeams.Add(1, Round1parts);
DictionaryOfTeams.Add(2, Round2parts);
DictionaryOfTeams.Add(3, Round3parts);
DictionaryOfTeams.Add(4, Round4parts);


Now I would like to serialize this object(DictionaryOfTeams) and store
it in the Db.

Your original question somehow indicated that the stream was the
only problem.

Assuming that you want to binary serialize not XML serialize, then
you need to look at the BinaryFormatter class.

See example below.

Arne

================================

using System;
using System.IO;
using System.Runtime.Serialization.Formatters.Binary;
using System.Collections.Generic;

namespace E
{
public class Ser<T>
{
public static byte[] Object2ByteArray(T o)
{
MemoryStream ms = new MemoryStream();
BinaryFormatter bf = new BinaryFormatter();
bf.Serialize(ms, o);
return ms.ToArray();
}
public static string Object2String(T o)
{
return Convert.ToBase64String(Object2ByteArray(o));
}
public static T ByteArray2Object(byte[] b)
{
MemoryStream ms = new MemoryStream(b);
BinaryFormatter bf = new BinaryFormatter();
ms.Position = 0;
return (T)bf.Deserialize(ms);
}
public static T String2Object(string s)
{
return ByteArray2Object(Convert.FromBase64String(s));
}
}
public class MainClass
{
public static void Main(string[] args)
{
DateTime dt1 = DateTime.Now;
Console.WriteLine(dt1);
DateTime dt2 =
Ser<DateTime>.String2Object(Ser<DateTime>.Object2String(dt1));
Console.WriteLine(dt2);
List<string> lst1 = new List<string>();
lst1.Add("A");
lst1.Add("BB");
lst1.Add("CCC");
foreach(string s in lst1) Console.WriteLine(s);
List<string> lst2 =
Ser<List<string>>.String2Object(Ser<List<string>>.Object2String(lst1));
foreach(string s in lst2) Console.WriteLine(s);
}
}
}
 
AMP said:
[...]
Stream stream = new MemoryStream();

Peter,
I probobly need a little more.

Then you need to actually ask for what you need. Unfortunately, simply
stating "I am lost" doesn't really convey what it is exactly you want
help with.

The Dictionary<TKey, TValue> class is in fact serializable, as is Array.
So it should work fine.

Be more specific. Include code.

Pete
 
Your are talking about DB and stream at the same time, which makes me
confused.

If what you need is just a persistent back-up of your dictionary in a
database, i would juste store each pair of key and value as tuple in a
table.

eg

KEY VALUE
1 ABCDEFGH
2 ABCD
.....

You can use the C# class SqlBulkInsert to write a dictionary in the
DB, and later juste sequentially read tuples with a SqlDataReader and
incrementally rebuilding your dictionary.

Guillaume
 
Your are talking about DB and stream at the same time, which makes me
confused.

If what you need is just a persistent back-up of your dictionary in a
database, i would juste store each pair of key and value as tuple in a
table.

eg

KEY     VALUE
1          ABCDEFGH
2          ABCD
....

You can use the C# class SqlBulkInsert to write a dictionary in the
DB, and later juste sequentially read tuples with a SqlDataReader and
incrementally rebuilding your dictionary.

Guillaume

I have added the following but the buf[] only has "0"'s in it ( The
length seems to be correct)
How do I read the memory stream from the beginning?


BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
char[] buf = new char[stre.Length];
StreamReader sr = new StreamReader(stre);
int bufsize = sr.Read(buf, 0, Convert.ToInt32
(stre.Length));

Thanks
 
AMP said:
I have added the following but the buf[] only has "0"'s in it ( The
length seems to be correct)
How do I read the memory stream from the beginning?

Try changing the stream definition to:
MemoryStream stre = new MemoryStream();

Then after the serialize call, just do stre.GetBuffer().

-Adam
 
reset the position of your memorystream by using stre.Seek(0,
SeekOrigin.Begin) before using the streamreader



Your are talking about DB and stream at the same time, which makes me
confused.

If what you need is just a persistent back-up of your dictionary in a
database, i would juste store each pair of key and value as tuple in a
table.

eg

KEY VALUE
1 ABCDEFGH
2 ABCD
....

You can use the C# class SqlBulkInsert to write a dictionary in the
DB, and later juste sequentially read tuples with a SqlDataReader and
incrementally rebuilding your dictionary.

Guillaume

I have added the following but the buf[] only has "0"'s in it ( The
length seems to be correct)
How do I read the memory stream from the beginning?


BinaryFormatter formatter = new BinaryFormatter();
Stream stre = new MemoryStream();
formatter.Serialize(stre, DictionaryOfTeams);
char[] buf = new char[stre.Length];
StreamReader sr = new StreamReader(stre);
int bufsize = sr.Read(buf, 0, Convert.ToInt32
(stre.Length));

Thanks
 
reset the position of your memorystream by using stre.Seek(0,
SeekOrigin.Begin)  before using the streamreader


Your are talking about DB and stream at the same time, which makes me
confused.
If what you need is just a persistent back-up of your dictionary in a
database, i would juste store each pair of key and value as tuple in a
table.

KEY VALUE
1 ABCDEFGH
2 ABCD
....
You can use the C# class SqlBulkInsert to write a dictionary in the
DB, and later juste sequentially read tuples with a SqlDataReader and
incrementally rebuilding your dictionary.
Guillaume

I have added the following but the buf[] only has "0"'s in it ( The
length seems to be correct)
How do I read the memory stream from the beginning?

            BinaryFormatter formatter = new BinaryFormatter();
            Stream stre = new MemoryStream();
            formatter.Serialize(stre, DictionaryOfTeams);
            char[] buf = new char[stre.Length];
            StreamReader sr = new StreamReader(stre);
            int bufsize = sr.Read(buf, 0, Convert.ToInt32
(stre.Length));

Thanks- Hide quoted text -

- Show quoted text -

Got it.
Thanks
 
AMP said:
reset the position of your memorystream by using stre.Seek(0,
SeekOrigin.Begin) before using the streamreader
[...]

Got it.

No, not really. You wrote the serialized data with BinaryFormatter.
But you are trying to read the serialized data as text. That is not
going to work.

Either use a text-based formatter (e.g. SoapFormatter), or encode the
resulting binary data as some reversible text format (e.g. Base64).

Pete
 
Back
Top