G
grossman.guy
Hi
I am trying to serialize using XmlSerializer dictionary of objects.
Some of the objects are generic classes.
When I serialize the dictionary I get an error: "There was an error
generating the XML document".
Here is the code:
[Serializable]
public struct Range<T>
{
public Range(T min, T max)
{
this.Min = min;
this.Max = max;
}
[XmlAttribute]
public T Min;
[XmlAttribute]
public T Max;
}
private void button9_Click(object sender, EventArgs e)
{
Dictionary<int, object> dic = new Dictionary<int, object>();
dic.Add(1, "key1 value");
Range<int> r = new Range<int>(0, 1);
dic.Add(2, r);
List<DictionaryEntry> lst = new List<DictionaryEntry>();
foreach (int key in dic.Keys)
{
lst.Add(new DictionaryEntry(key, dic[key]));
}
XmlSerializer xmlSerializer = new
XmlSerializer(typeof(List<DictionaryEntry>));
MemoryStream stream = new MemoryStream();
xmlSerializer.Serialize(stream, lst);
}
Is it possible to serialize generic classes using XmlSerializer? If
yes, what is the problem of my code?
Thanks
Guy
I am trying to serialize using XmlSerializer dictionary of objects.
Some of the objects are generic classes.
When I serialize the dictionary I get an error: "There was an error
generating the XML document".
Here is the code:
[Serializable]
public struct Range<T>
{
public Range(T min, T max)
{
this.Min = min;
this.Max = max;
}
[XmlAttribute]
public T Min;
[XmlAttribute]
public T Max;
}
private void button9_Click(object sender, EventArgs e)
{
Dictionary<int, object> dic = new Dictionary<int, object>();
dic.Add(1, "key1 value");
Range<int> r = new Range<int>(0, 1);
dic.Add(2, r);
List<DictionaryEntry> lst = new List<DictionaryEntry>();
foreach (int key in dic.Keys)
{
lst.Add(new DictionaryEntry(key, dic[key]));
}
XmlSerializer xmlSerializer = new
XmlSerializer(typeof(List<DictionaryEntry>));
MemoryStream stream = new MemoryStream();
xmlSerializer.Serialize(stream, lst);
}
Is it possible to serialize generic classes using XmlSerializer? If
yes, what is the problem of my code?
Thanks
Guy