CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 4 of 4
  1. #1
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    Unhappy 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.

  2. #2
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: XML serialization woes regarding collections

    Quote Originally Posted by Eri523 View Post
    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

  3. #3
    Join Date
    Jun 2010
    Location
    Germany
    Posts
    2,675

    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.

    (You see, you somtimes get appreciation despite the TIA in the OP. )

    (... and I still prefer Altbier... )
    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.

  4. #4
    Join Date
    Oct 2008
    Location
    Cologne, Germany
    Posts
    756

    Re: XML serialization woes regarding collections

    Quote Originally Posted by Eri523 View Post
    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:

    Quote Originally Posted by Eri523 View Post
    (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
  •  





Click Here to Expand Forum to Full Width

Featured