473,320 Members | 2,041 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,320 software developers and data experts.

xml serializer treatment of collection objects

If I understand the Xml Serializer correctly, as it is
serializing an object graph, if it comes across a
collection object, it puts a tag in for the collection and
then tries to serialize all of the members of the
collection. This is roughly the same treatment when the
top of my object graph is itself a collection. I'm
curious, however, about 2 things with regard to passing a
custom collection object to the XmlSerializer:

1) Why cannot I not use the XmlRootAttribute on my
collection object to specify a custom tag name for my
collection?
2) What exactly does the XmlSerializer do in order to make
the determination that an object is a collection? I
looked through Anakrino, but didn't see anything specific
to this.

thanks,

_howard
Nov 11 '05 #1
3 2131
howard dierking wrote:
1) Why cannot I not use the XmlRootAttribute on my
collection object to specify a custom tag name for my
collection? Because XmlRootAttribute is supposed to mark class, struct, enumeration, or
interface only, to control serialization of root element in other words.
2) What exactly does the XmlSerializer do in order to make
the determination that an object is a collection? I
looked through Anakrino, but didn't see anything specific
to this.

From XmlSerializer class documentation in MSDN:
"Note The XmlSerializer gives special treatment to classes that implement
IEnumerable or ICollection. A class that implements IEnumerable must implement
a public Add method that takes a single parameter. The Add method's parameter
must be of the same type as is returned from the Current property on the value
returned from GetEnumerator, or one of that type's bases. A class that
implements ICollection (such as CollectionBase) in addition to IEnumerable
must have a public Item indexed property (indexer in C#) that takes an
integer, and it must have a public Count property of type integer. The
parameter to the Add method must be the same type as is returned from the Item
property, or one of that type's bases. For classes implementing ICollection,
values to be serialized will be retrieved from the indexed Item property, not
by calling GetEnumerator."

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #2
yea, I read the MSDN library too. Let me try and rephrase:

1) Why can't I use the XmlRootAttribute on my collection class which I
derive from CollectionBase? In my previously mentioned example, my
collection class was my root element as I was not using it as a property of
another class.

2) internally, doe the XmlSerializer look for the ICollection or IEnuerable
interfaces and use that is its decision making fact, or is there something
else?

_howard
"Oleg Tkachenko" <oleg@NO_SPAM_PLEASEtkachenko.com> wrote in message
news:OJ**************@TK2MSFTNGP09.phx.gbl...
howard dierking wrote:
1) Why cannot I not use the XmlRootAttribute on my
collection object to specify a custom tag name for my
collection? Because XmlRootAttribute is supposed to mark class, struct, enumeration,

or interface only, to control serialization of root element in other words.
2) What exactly does the XmlSerializer do in order to make
the determination that an object is a collection? I
looked through Anakrino, but didn't see anything specific
to this. From XmlSerializer class documentation in MSDN:
"Note The XmlSerializer gives special treatment to classes that

implement IEnumerable or ICollection. A class that implements IEnumerable must implement a public Add method that takes a single parameter. The Add method's parameter must be of the same type as is returned from the Current property on the value returned from GetEnumerator, or one of that type's bases. A class that
implements ICollection (such as CollectionBase) in addition to IEnumerable
must have a public Item indexed property (indexer in C#) that takes an
integer, and it must have a public Count property of type integer. The
parameter to the Add method must be the same type as is returned from the Item property, or one of that type's bases. For classes implementing ICollection, values to be serialized will be retrieved from the indexed Item property, not by calling GetEnumerator."

--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #3
Howard Dierking wrote:
1) Why can't I use the XmlRootAttribute on my collection class which I
derive from CollectionBase? In my previously mentioned example, my
collection class was my root element as I was not using it as a property of
another class. I believe it's sort of limitation.
2) internally, doe the XmlSerializer look for the ICollection or IEnuerable
interfaces and use that is its decision making fact, or is there something
else?

afaik XmlSerializer detects only Array, ICollection-compatible and
IEnumerable-compatible classes.
--
Oleg Tkachenko
http://www.tkachenko.com/blog
Multiconn Technologies, Israel

Nov 11 '05 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: | last post by:
Hi, When we get generic collections will the serializer change so we dont need to give it a type parameter for collections ? Currently today if we serialize an ArrayList, we have to give it a...
5
by: Tappy Tibbons | last post by:
I have a class I am serializing, and need the resultant XML to skip/omit classes that are not initialized, or their member variables have not been set. Is this possible? Say for the following...
1
by: Tappy Tibbons | last post by:
I have been trying everything I can find to add a collection to a class that will be run through the XML Serializer, and am coming up with nothing THAT WORKS. Can anyone help point me in the right...
10
by: aa7im | last post by:
I have a similiar thread going about this topic but I decided to break it off into a seperate discussion. Question: What is the best way to determine if an object "IsNew" to a collection? ...
3
by: JJ | last post by:
Hi, I noticed in a sample app source code that the app made use of a class for example a user class and then had the user objects that got created stuffed into a user collection. I was wondering...
5
by: | last post by:
Trying to learn about manipulating collections of objects, and populating these objects dynamically from datasources. Could someone post a code sample that shows the following: Instantiating a...
10
by: Chet Cromer | last post by:
I am creating a set of base classes and sub classes to use throughout a program I'm developing. The base class represents a generic "lookup table" from my database that contains lists of things...
158
by: pushpakulkar | last post by:
Hi all, Is garbage collection possible in C++. It doesn't come as part of language support. Is there any specific reason for the same due to the way the language is designed. Or it is...
0
by: DolphinDB | last post by:
Tired of spending countless mintues downsampling your data? Look no further! In this article, you’ll learn how to efficiently downsample 6.48 billion high-frequency records to 61 million...
0
by: ryjfgjl | last post by:
ExcelToDatabase: batch import excel into database automatically...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: jfyes | last post by:
As a hardware engineer, after seeing that CEIWEI recently released a new tool for Modbus RTU Over TCP/UDP filtering and monitoring, I actively went to its official website to take a look. It turned...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
0
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 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 former...

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.