473,842 Members | 1,575 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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 9046
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******** ********@TK2MSF TNGP12.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("Collec tion")]
public class joaCollection : joaItem, IEnumerable
{
//...

}

Results in the following error...

System.InvalidO perationExcepti on: There was an error reflecting type
'DotnetBench.jo aCollection'. ---> System.InvalidO perationExcepti on: XML
attributes may not be specified for the type DotnetBench.joa Collection.

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************ *******@nospamh otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP12.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************ *******@nospamh otmail.com> wrote in message
news:%2******** ********@TK2MSF TNGP09.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******** ********@TK2MSF TNGP12.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 thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

8
3573
by: Harris Boyce | last post by:
Hello, I'm trying to use the FOR XML EXPLICIT clause with SQL Server to deserialize data from my database into a strongly-typed collection object that I will use throughout my application. I initially tested my design by building a collection in code and then serializing it to/from an XML file, which worked fine. However, I have hit a brick wall trying to restore the data from SQL Server. I originally had my collection and object
2
2362
by: Sebastian Sylvan | last post by:
Hello. I'm trying to serialize an object using the XML-serializer. A simple object is okay, But when I try to serialize an object which has a public List<SomeType> the program hangs upon calling the XmlSerializer constructor. I have verified that everything in the object is okay (not null). I've tried to give the list a type of IEnumerable instead of List<T> but that didn't work either. I'm using the Visual C# Express Beta.
10
2763
by: jcc | last post by:
Hi guys, I'm a newbie to C#. My Visual Studio 2005 failed to compile the following code with error as 'HelloWorld.A' does not implement interface member 'System.Collections.IEnumerable.GetEnumerator()'. 'HelloWorld.A.GetEnumerator()' is either static, not public, or has the wrong return type. class A : IEnumerable<string>
5
4212
by: Tin Gherdanarra | last post by:
Dear mpdls, here is a simple example of an IEnumerable that generates integers: It works, but I have only a vague idea of what's going on. I understand that /yield/ wraps the humble integer that comes from counter++
4
1947
by: cwertman | last post by:
Ok the XmlSerializer is nice for my need. BUT I what I need (maybe the XmlSerializer isnt what I need) is to be able to take the following objects A Person who has a collection of Addresses Person (fname)
2
4242
by: =?Utf-8?B?a2VubmV0aEBub3NwYW0ubm9zcGFt?= | last post by:
When creating multiple iterators, the original is defined as returning IEnumerator, ie public IEnumerator GetEnumerator() { yield x; ...} whereas the additional ones are defined as returning IEnumerable, ie public IEnumerable AnotherSortOrder() { yield x;....} Any insights out there as to why the additional iteration methods did not just return IEnumerator? (its just a little confusing, hoping for some better insight)
2
4588
by: Morgan Cheng | last post by:
In .Net 2.0, Generics are introduced. I found that IEnumerable<T> inherites from IEnumerable. This makes implementation of IEnumerable<Thas to have two GetEnumerator methods defined( one for IEnumerable<Tand the other for IEnumerable). I doubt why .Net class hierarchy is designed in such a way. IMHO, they should not have inheritance releationship, just like IList<Tand IList. I googled the web and found two related articles....
12
2414
by: gnewsgroup | last post by:
I've read the msdn doc about IEnumerable. It seems to me that IEnumerable objects are essentially wrapped-up arrays. It simply gives us the foreach convenience. Is this correct?
2
2506
by: Tony Johansson | last post by:
Hello! Below I have a working program. I have one generic class called Farm<T> with this header definition public class Farm<T: IEnumerable<Twhere T : Animal Now to my question I changed the inheritance of the IEnumerable from the generic IEnumerable<T> to the generel IEnumerable and the program function just the same so no
0
10945
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10612
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
10672
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
10314
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
9453
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7858
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5696
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5885
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
4499
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.