471,342 Members | 1,957 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 471,342 software developers and data experts.

XmlSerialization for generics

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, objectdic = new Dictionary<int, object>();

dic.Add(1, "key1 value");

Range<intr = new Range<int>(0, 1);

dic.Add(2, r);

List<DictionaryEntrylst = 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
Aug 14 '08 #1
3 1465
On Aug 14, 12:56*pm, grossman....@gmail.com wrote:
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, objectdic = new Dictionary<int, object>();

* * * * dic.Add(1, "key1 value");

* * * * Range<intr = new Range<int>(0, 1);

* * * * dic.Add(2, r);

* * * * List<DictionaryEntrylst = 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?
It might have something to do with DictionaryEntry.Key and
DictionaryEntry.Value both having type Object - XmlSerializer cannot
handle polymorphism without explicit annotations. Anyway, you should
look at InnerException - it will typically give a more detailed error
description. For XmlSerializer in particular, it may be needed to dig
down through several layers of inner exceptions to get to the root
cause of error; but once you get there, it is pretty clear.

By the way, [Serializable] attribute is not needed here (it's not used
by XML serialization).
Aug 14 '08 #2
On Aug 14, 1:45*pm, Pavel Minaev <int...@gmail.comwrote:
On Aug 14, 12:56*pm, grossman....@gmail.com wrote:


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, objectdic = new Dictionary<int, object>();
* * * * dic.Add(1, "key1 value");
* * * * Range<intr = new Range<int>(0, 1);
* * * * dic.Add(2, r);
* * * * List<DictionaryEntrylst = 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?

It might have something to do with DictionaryEntry.Key and
DictionaryEntry.Value both having type Object - XmlSerializer cannot
handle polymorphism without explicit annotations. Anyway, you should
look at InnerException - it will typically give a more detailed error
description. For XmlSerializer in particular, it may be needed to dig
down through several layers of inner exceptions to get to the root
cause of error; but once you get there, it is pretty clear.

By the way, [Serializable] attribute is not needed here (it's not used
by XML serialization).- Hide quoted text -

- Show quoted text -
Thanks Pavel.

The inner exception was very helpful.

Guy
Aug 14 '08 #3
On Aug 14, 2:24*pm, grossman....@gmail.com wrote:
On Aug 14, 1:45*pm, Pavel Minaev <int...@gmail.comwrote:


On Aug 14, 12:56*pm, grossman....@gmail.com wrote:
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, objectdic = new Dictionary<int, object>();
* * * * dic.Add(1, "key1 value");
* * * * Range<intr = new Range<int>(0, 1);
* * * * dic.Add(2, r);
* * * * List<DictionaryEntrylst = 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?
It might have something to do with DictionaryEntry.Key and
DictionaryEntry.Value both having type Object - XmlSerializer cannot
handle polymorphism without explicit annotations. Anyway, you should
look at InnerException - it will typically give a more detailed error
description. For XmlSerializer in particular, it may be needed to dig
down through several layers of inner exceptions to get to the root
cause of error; but once you get there, it is pretty clear.
By the way, [Serializable] attribute is not needed here (it's not used
by XML serialization).- Hide quoted text -
- Show quoted text -

Thanks Pavel.

The inner exception was very helpful.

Guy- Hide quoted text -

- Show quoted text -
I solved the problem in the following way:
Instead of DictionaryEntry I used a similar class named Entry and
declared it as following:

[XmlInclude(typeof(Range<int>))]
public struct Entry
{
...
}

Guy
Aug 14 '08 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

2 posts views Thread by STom | last post: by
reply views Thread by A programmer desperatly needing help! | last post: by
23 posts views Thread by Luc Vaillant | last post: by
9 posts views Thread by sloan | last post: by
3 posts views Thread by LW | last post: by
13 posts views Thread by rkausch | last post: by

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.