I recently was trying to create a class (a subclassed EventArgs class in my case) where I needed the class to contain a property which housed a few named valued pairs.Additionally, I wanted to be able to XmlSerialize the class also, so I could store it for later retrieval. The reasons for that are not really that important.

Of course, when one thinks of named value pairs, something like a Hashtable comes to mind.So there I went off coding, making a class, which contained one public property, which was a hashtable.

So I said "No problem, the value part of Hashtable can be complex objects, and all I need is a some string (keys) and some string (values).So I’ll create a "Item" object, and then a "ItemCollection" object which implements CollectionBase. Then I’ll serialize that the collection object. Surely that can’t be an issue.

So let me be the first one to say that I really haven’t "discovered" anything new, I’m just relaying my struggle with it, and some information that I found. By reading other sites, you can probably discern the same things I have in this particuliar area. I have tried to layout some examples so that you can get a better feel for what is happening. And if you’re trying to just figure out some XmlSerialization in general, the examples are a good starting point.

I did "bring together" 2 different inputs, in regards to the CollectionBase serialization, and Nitin Pande‘s article on encoding. When I was saving the xml to a database (text object in sql server 2000), I was finding that the encoding was switching up on me, unless I explicitly set it.

Any, on to my "Non-Discoveries" (aka, summing up what I figured out from others).

The issue is that everything works, except you can’t put an Xml tag on the collection declaration.Here is a snipplet:

Which is what Matt is talking about at his (second) post listed above.Thus he figured out that at least he could put the MyCollection in a Wrapper object.

I’m with him when he says "All in all, I dont see why the serialization of collections is so different for collections as opposed to other classes." I don’t get on the "that is a bug/oversite" wagon too often, but this one might be it.The exact error is "There was an error reflecting type" and then lists the MyCollection object afterwards (that’s for googlers trying to find the exact phrase match).

I actually thought I had figured something out, but turns out I just had a DataContainer myself, it just wasn’t as apparent at first.

Anyway. Thanks Matt for the DataContainer idea.

For the rest of you, I’ve composed a little example C# solution to try and figure some of this stuff out. (Again, I haven’t "discovered" anything new).I have purposely tried to name things differently, and not to standard, so you can see what is going on.I find that sometimes the code becomes very ambigious when the same names are used over and over again.You should be able to figure out how to XmlSerialize an:ObjectObjectCollectionAn ObjectTwo which houses an ObjectCollectionand finally a ObjectTwoCollection which hold multiple ObjectTwo’s, (and the ObjectTwo’s contain the original ObjectCollection).Let the example do the talking, and you’ll see what I mean.

Back to my original issue, which was I needed a subclassed EventArgs, which contained a (sub)Collection of key/value pairs (the key and value were just strings in this case).(If you’re looking to do something like this, just replace StateObject.StateAbbrevation and StateObject.StateFullname with "KeyValuePair.Key and KeyValuePair.Value" , and that’s about what I have in my code)

I was able to do this, and in essence my (subclassed)EventArgs class became what Matt is calling the DataContainer.

My conclusion is that you CAN XmlSerialize a custom (CollectionBase) object, you just can’t give it the Xml name you want, UNLESS you do the Matt DataContainer work around.

The sample code will hopefully help you get a better understanding of what is going on.

My need was solved, and now I have a XmlSerialized version of my EventArgs class.And I learned more about how XmlSerialization works and doesn’t work.I might email the CRL team at Microsoft and ask them "What gives?"

[System.Xml.Serialization.XmlIgnoreAttribute()] //I just threw this in to complete the example a little more, with an ignore attribute public string PropertyWhichWontBeSerialized { get{return this.m_secretInformation; } set{this.m_secretInformation = value;} }

[Serializable] // ***** The next line is the issue. When you uncomment, you get the Reflection error. // see comment in the CollectionOfStatesObjectsExample procedure //[System.Xml.Serialization.XmlRootAttribute("PersonCollectionRootXmlNode")]

namespace GranadaCoder.Applications.CollectionBaseXmlSerializeExampleCS.Collections{ /// <summary> /// Summary description for PersonCollectionWithArraySubCollection. /// This really isn’t a collection, so roll with the punches on this one. /// </summary> ///

//Now this works, but relies on an Array (not to be confused with an ArrayList) of PersonObjects. [Serializable] [System.Xml.Serialization.XmlRootAttribute("PersonCollWithArrayRootXmlNode")] public class PersonCollectionWithArraySubCollection {

// ***** The next line is the issue. When you uncomment, you get the Reflection error. //see the comment in the CollectionOfStatesObjectsExample procedure //[System.Xml.Serialization.XmlRootAttribute("StateCollectionRootXmlNode")]

namespace GranadaCoder.Applications.CollectionBaseXmlSerializeExampleCS.CollectionWrappers{ /// <summary> /// Summary description for StateCollectionWrapperForXmlSerialization. /// this really isn’t a collection, so roll with the punches /// </summary> /// [Serializable] //[System.Xml.Serialization.XmlRootAttribute("StateCollectionWrapper")] // this might work too (XmlRootAttribute instead of XmlRoot), if you uncomment this line AND comment out the line immediately below [System.Xml.Serialization.XmlRoot ("WrapperRoot")] public class StateCollectionWrapperForXmlSerialization {

//Notice the output here //specifically "ArrayOfStateObject" //this is the name .NET gives it //this is the issue, when you try to say "I want to call it something else" //you get the reflection error. //Notice that "ArrayOfPersonObject" when the CollectionOfPersonObjectsExample method runs