-
November 25th, 2010, 09:24 PM
#1
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.
-
December 10th, 2010, 03:47 PM
#2
Re: XML serialization woes regarding collections
Originally Posted by Eri523
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?
I gave serialization up a long time ago. currently I use LINQ for reading and writing custom xml-configs.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
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
-
December 13th, 2010, 10:54 AM
#3
-
December 13th, 2010, 01:19 PM
#4
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:
Code:
public static class Settings
{
public static class Directories
{
public static String Dir1 {...}
public static String Dir2 {...}
public static String Dir3 {...}
}
public static class Drawing
{
public static class Pens
{
public static class Pen1 {...}
public static class Pen1 {...}
public static class Pen1 {...}
}
}
}
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.
EDIT:
Originally Posted by Eri523
(You see, you somtimes get appreciation despite the TIA in the OP. )
aaah, now I get it thank you
Last edited by memeloo; December 13th, 2010 at 01:23 PM.
win7 x86, VS 2008 & 2010, C++/CLI, C#, .NET 3.5 & 4.0, VB.NET, VBA... WPF is comming
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
Tags for this Thread
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|