XmlSerialization for generics


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
 
Ad

Advertisements

P

Pavel Minaev

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?

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).
 
G

grossman.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?

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
 
Ad

Advertisements

G

grossman.guy

On Aug 14, 12:56 pm, (e-mail address removed) 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, 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?
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
 

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