Lexel Shared Source and Xml Serialization
Been working on some code that I’m going to be sharing with the world. Right now it has two pieces. The first is some code to go fetch data from ZHubert’s xml. That’s almost done. I’m also adding in some classes for creating and RSS feed, which I’ll be using for the NET GEMS project. That actually creates a valid rss xml file now, though not the whole 2.0 specification. Not that it’s a big spec. Should take only a few hours to finish.
Anyway...so instead of building the Xml manually using an XmlTextWriter or the XmlDocument class, I’m doing the whole thing through the Xml Serialization functionality of the .NET framework. I found in this article how to serialize an array of objects as a flat series of xml items, but it doesn’t say how to do the same with an ArrayList, which is what I would like to do. To do it with an array all you have to do is this:
[XmlElement] public RSSItem ItemArray;
But that won’t work for an ArrayList, because the serializer needs to know the type of the object that is in the ArrayList to serialize it. So you may be thinking, "just do this":
[XmlElement] [XmlElement(typeof(RSSItem))] public ArrayList Items;
Nope. The element name for the items ends up being the class name, which is RSSItem. But how about this?
[XmlElement("item")] [XmlElement(typeof(RSSItem))] public ArrayList Items;
Nope. Same result. I also have the RSSItem class declaration with a "[XmlRoot("item")]", but that doesn’t seem to help.
As is, I’ve got a work around to deal with this but it feels like too much of a hack. If anyone has any suggestions, I’m open. I’m sure I’m missing something pretty obvious here.
[Update 2/7/2005, 8:00]
Figured it out. I just had to declare my ArrayList like this:
[XmlElement(typeof(RSSItem), ElementName="item")] public ArrayList Items;
Voila! I can’t think of why the other options didn’t work. But, oh well.