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);
}
}
}