XmlSerializer plays very nicely with types that implement IEnumerable, and/or ICollection.
The best way to illustrate this is by example. Consider the structure of
RSS 2.0, which specifies a
sequence of <item> elements as direct children of the <channel>
-- right alongside all the other channel-level properties, like <copyright>
and <lastBuildDate>. (The point being: there is no enclosing
<items> or <itemcollection> tag to mark the begin and end of the sequence of <item>
elements.)
Here's a snippet from my Jitsu.Rss.Serialization classes, which demonstrates what the XML serialization code must look like, in order to implement that -- pay special attention to the last member field:
public class RssChannel { [XmlElement] public string title; [XmlElement] public string link; [XmlElement] public string description; [XmlElement] public string copyright; [XmlIgnore] public DateTime lastBuildDate; [XmlElement("lastBuildDate")] public string lastBuildDate_xml { get { return lastBuildDate.ToString("R"); } set { lastBuildDate = DateTime.ParseExact(value,"R",DateTimeFormatInfo.InvariantInfo); } } [XmlElement] public string generator; [XmlElement("item",typeof(RssItem))] public ArrayList items;
When XmlSerializer sees a member that implements IEnumerable (like ArrayList, in the example above), it effectively does a 'foreach' loop over it, to read or write multiple items... Pretty nifty, eh?
