473,223 Members | 1,792 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,223 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 1513
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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

2
by: STom | last post by:
I have just started reading up on XMLSerialization and still do not understand the practical use of this technology. For example, if I have to know the class type on the client and on the web...
0
by: A programmer desperatly needing help! | last post by:
I use the xmlserialization on asp.net pages and on previous machines it never gave a problem. But now i somethings get a: Timed out waiting for a program to execute. The command being executed was...
23
by: Luc Vaillant | last post by:
I need to initialise a typed parameter depending of its type in a generic class. I have tried to use the C++ template form as follow, but it doesn't work. It seems to be a limitation of generics...
12
by: Michael S | last post by:
Why do people spend so much time writing complex generic types? for fun? to learn? for use? I think of generics like I do about operator overloading. Great to have as a language-feature, as...
4
by: pfrisbie | last post by:
I am developing a Web Services interface with C# and our partner is using Java (Axis 1.1). They require me to include xsi:types in the SOAP Messages I send them. For example: <Partner...
9
by: sloan | last post by:
I'm not the sharpest knife in the drawer, but not a dummy either. I'm looking for a good book which goes over Generics in great detail. and to have as a reference book on my shelf. Personal...
3
by: LW | last post by:
Hi! I am getting the following error message for my fairly simple web service. I have classes and have two DataSets that reference the same classes. The error is: The XML element named...
1
by: Frank | last post by:
Hi, Let's say I have a file named myFile.xml Within that file I have blocks of data which I'd like to add at different times during the day. e.g. <LogEntry>
13
by: rkausch | last post by:
Hello everyone, I'm writing because I'm frustrated with the implementation of C#'s generics, and need a workaround. I come from a Java background, and am currently writing a portion of an...
0
by: IanWright | last post by:
This is a little bit more of an advanced topic for serialization. For those who don't know what XML serialization is, then this probably isn't for you just yet. For those that do, you may have come...
1
isladogs
by: isladogs | last post by:
The next online meeting of the Access Europe User Group will be on Wednesday 6 Dec 2023 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, Mike...
0
by: VivesProcSPL | last post by:
Obviously, one of the original purposes of SQL is to make data query processing easy. The language uses many English-like terms and syntax in an effort to make it easy to learn, particularly for...
3
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 3 Jan 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). For other local times, please check World Time Buddy In...
0
by: mar23 | last post by:
Here's the situation. I have a form called frmDiceInventory with subform called subfrmDice. The subform's control source is linked to a query called qryDiceInventory. I've been trying to pick up the...
0
by: abbasky | last post by:
### Vandf component communication method one: data sharing ​ Vandf components can achieve data exchange through data sharing, state sharing, events, and other methods. Vandf's data exchange method...
2
by: jimatqsi | last post by:
The boss wants the word "CONFIDENTIAL" overlaying certain reports. He wants it large, slanted across the page, on every page, very light gray, outlined letters, not block letters. I thought Word Art...
2
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 7 Feb 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:30 (7.30PM). In this month's session, the creator of the excellent VBE...
0
by: stefan129 | last post by:
Hey forum members, I'm exploring options for SSL certificates for multiple domains. Has anyone had experience with multi-domain SSL certificates? Any recommendations on reliable providers or specific...
0
by: MeoLessi9 | last post by:
I have VirtualBox installed on Windows 11 and now I would like to install Kali on a virtual machine. However, on the official website, I see two options: "Installer images" and "Virtual machines"....

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.