If this is your first visit, be sure to
check out the FAQ by clicking the
link above. You may have to register
before you can post: click the register link above to proceed. To start viewing messages,
select the forum that you want to visit from the selection below.

Hybrid View

XML serialization woes regarding collections

This is a spin-off of http://www.codeguru.com/forum/showthread.php?t=504635, in particular posts #10 and #12. Those who might be interested in the history of why I posted this might have a look over there. I have detached it from there as it could be of interest by itself.

In order to produce the XML Arjay has asked for, I did some experiments on XML serialization, and it appears to summon a host of new problems.

It turned out to be really tricky to serialize collections. According to MSDN, a collection can't be serialized if it's either an ArrayList or a List<T>. As I found out during my experiments by examining the exceptions I got, for some obscure (at leat to me) reasons it also can't be serialized if it implements IDictionary, what I didn't find mentioned in the docs. But for what purpose the heck then does Dictionary<TKey, TValue> implement ISeializable as well? These conditions together rule out a whole lot of the available collections when it comes to serialization.

During my desperate search for a candidate I came across SortedSet<T>, which doesn't really fit the concept of the collection I was looking for, though could be twisted to fit in a pinch. But it gets ruled out by another condition mentioned on the MSDN page I linked to above: If it does implement ICollection (which it does) it needs to have a public indexer that takes an int (which it does not).

And, BTW, the ConfigurationProperty class isn't serializable either, as it doesn't have a default constructor. (The configuration infrastructure somehow manages to store it to the config files, but it is not accepted by XmlSerializer.)

I feel that serialization really isn't that much fun if it is that hard to use it for collections. Or did I fall to some fundamental misunderstanding here?

These exceptions that get thrown by the CLR types involvd here can be really informative if examined carefully. That's nice, but it doesn't really help me out either...

Yes, I know this is a post with maybe too many s. But maybe someone can help me to get out of this calamity.

TIA

I was thrown out of college for cheating on the metaphysics exam; I looked into the soul of the boy sitting next to me.

This is a snakeskin jacket! And for me it's a symbol of my individuality, and my belief... in personal freedom.

remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

private lessons are not an option so please don't ask for help in private, I won't replay

if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

Re: XML serialization woes regarding collections

Thanks for pointing me to LINQ. I didn't have much more than a brief look on it yet, but it looks really promising in some aspects.

Incidentally, Arjay posted an awesome piece of code over there in the config thread, just 2½ hours before your post here. He actually used a tiny bit of LINQ too, but mainly his code is based on XML serialization. What he posted there seems to fit my needs just like a glove, so I'll try this approach first.

But one aspect that made LINQ really appealing to me is that it seems to be not too convoluted to export/import some data to/from Excel using it. This is not of much use for the config stuff, but it could make up a nice piece of sugar on top of another part of my app. I'll definitely keep looking into that.

Re: XML serialization woes regarding collections

Originally Posted by Eri523

Incidentally, Arjay posted an awesome piece of code over there in the config thread, just 2½ hours before your post here. He actually used a tiny bit of LINQ too, but mainly his code is based on XML serialization. What he posted there seems to fit my needs just like a glove, so I'll try this approach first.

what I don't like about serialization is that it's difficult to nest classes (or I missed something)
I mean for example something like this:

here in the getters and setters I can directly write code to get or set settings but with serialization the class has to be instantiated and the subclasses have to be properties that you also must instantiate because as far as I know serialization works only with instances.

the only one drawback is that I had to write a routine that adds non existing elements or attributes dynamicaly when you specify a XPath.

remeber to give feedback you think my response deserves recognition? perhaps you may want to click the Rate this post link/button and add to my reputation

private lessons are not an option so please don't ask for help in private, I won't replay

if you use Opera and you'd like to have the tab-button functionality for the texteditor take a look at my Opera Tab-UserScirpt; and if you know how to stop firefox from jumping to the next control when you hit tab let me know

* The Perfect Platform for Game Developers: Android
Developing rich, high performance Android games from the ground up is a daunting task. Intel has provided Android developers with a number of tools that can be leveraged by Android game developers.

* The Best Reasons to Target Windows 8
Learn some of the best reasons why you should seriously consider bringing your Android mobile development expertise to bear on the Windows 8 platform.