472,145 Members | 1,484 Online
Bytes | Software Development & Data Engineering Community
Post +

Home Posts Topics Members FAQ

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

XmlSerializer and IEnumerable

I'm trying to serialize a class with XmlSerializer. This class implements
the IEnumerable interface. I implemented the IEnumerable interface for
reasons other than Xml serialization.

However I find that the XmlSerializer, because I have an IEnumerable
interface on this class, wants to ignore the public properties of this class
and simply serialize an array of objects.

I wand XmlSerializer to ignore the IEnumerable interface that I have
implemented on this class. How do I do that?

Regards,
Bob Rundle
Nov 12 '05 #1
3 8794
Have you marked your properties with [XmlAttribute] and/or [XmlElement]?
Those are "hints" to the deserializer/serializer as to what/how do convert.
I wrote a custom deserializer that can handle abstract/concrete types and
had to use those attributes + write a few of my own extensively.
De/serialization can automatically handle some objects, but when you start
getting more complex objects, those attributes are indispensible.
"Bob Rundle" <ru****@rundle.com> wrote in message
news:eo****************@TK2MSFTNGP12.phx.gbl...
I'm trying to serialize a class with XmlSerializer. This class implements
the IEnumerable interface. I implemented the IEnumerable interface for
reasons other than Xml serialization.

However I find that the XmlSerializer, because I have an IEnumerable
interface on this class, wants to ignore the public properties of this class and simply serialize an array of objects.

I wand XmlSerializer to ignore the IEnumerable interface that I have
implemented on this class. How do I do that?

Regards,
Bob Rundle

Nov 12 '05 #2
Keith,

Yes I am using XmlAttribute and XmlElement attributes.

If a class implements IEnumerable however, you cannot add XML attributes to
it.

For example

[XmlType("Collection")]
public class joaCollection : joaItem, IEnumerable
{
//...

}

Results in the following error...

System.InvalidOperationException: There was an error reflecting type
'DotnetBench.joaCollection'. ---> System.InvalidOperationException: XML
attributes may not be specified for the type DotnetBench.joaCollection.

I also found a reference that says that this behavoir is "by design". Whose
idiotic design is this?

I am very unhappy about this. The reason I want to use IEnumerable is so my
collection can be used in foreach statements. However it appears that if I
want to have a few XML serialized properties in my class I will need to
abandon IEnumerable!

Thanks for the help,
Bob Rundle

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Have you marked your properties with [XmlAttribute] and/or [XmlElement]?
Those are "hints" to the deserializer/serializer as to what/how do convert. I wrote a custom deserializer that can handle abstract/concrete types and
had to use those attributes + write a few of my own extensively.
De/serialization can automatically handle some objects, but when you start
getting more complex objects, those attributes are indispensible.
"Bob Rundle" <ru****@rundle.com> wrote in message
news:eo****************@TK2MSFTNGP12.phx.gbl...
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for
reasons other than Xml serialization.

However I find that the XmlSerializer, because I have an IEnumerable
interface on this class, wants to ignore the public properties of this

class
and simply serialize an array of objects.

I wand XmlSerializer to ignore the IEnumerable interface that I have
implemented on this class. How do I do that?

Regards,
Bob Rundle


Nov 12 '05 #3
Keith,

I'm curious about your custom deserializer. I've finally given up on
XmlSerialize() and will have to write my own serialization. I'm planning to
add a Serialize() method to each of my classes to (de)serialize the
individal class. Is this the proper approach or is there another pattern to
follow?

Regards,
Bob Rundle

"Keith Patrick" <ri*******************@nospamhotmail.com> wrote in message
news:%2****************@TK2MSFTNGP09.phx.gbl...
Have you marked your properties with [XmlAttribute] and/or [XmlElement]?
Those are "hints" to the deserializer/serializer as to what/how do convert. I wrote a custom deserializer that can handle abstract/concrete types and
had to use those attributes + write a few of my own extensively.
De/serialization can automatically handle some objects, but when you start
getting more complex objects, those attributes are indispensible.
"Bob Rundle" <ru****@rundle.com> wrote in message
news:eo****************@TK2MSFTNGP12.phx.gbl...
I'm trying to serialize a class with XmlSerializer. This class implements the IEnumerable interface. I implemented the IEnumerable interface for
reasons other than Xml serialization.

However I find that the XmlSerializer, because I have an IEnumerable
interface on this class, wants to ignore the public properties of this

class
and simply serialize an array of objects.

I wand XmlSerializer to ignore the IEnumerable interface that I have
implemented on this class. How do I do that?

Regards,
Bob Rundle


Nov 12 '05 #4

This discussion thread is closed

Replies have been disabled for this discussion.

Similar topics

8 posts views Thread by Harris Boyce | last post: by
2 posts views Thread by Sebastian Sylvan | last post: by
10 posts views Thread by jcc | last post: by
5 posts views Thread by Tin Gherdanarra | last post: by
2 posts views Thread by =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post: by
2 posts views Thread by Morgan Cheng | last post: by
reply views Thread by leo001 | 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.