XmlSerialize a custom collection with an Attribute

Collections generally don’t make good places for extra properties. Both during serialization and in data-binding, they will be ignored if the item looks like a collection (IList, IEnumerable, etc – depending on the scenario).

If it was me, I would encapsulate the collection – i.e.

[Serializable]
public class MyCollectionWrapper {
    [XmlAttribute]
    public string SomeProp {get;set;} // custom props etc
    [XmlAttribute]
    public int SomeOtherProp {get;set;} // custom props etc
    public Collection<string> Items {get;set;} // the items
}

The other option is to implement IXmlSerializable (quite a lot of work), but that still won’t work for data-binding etc. Basically, this isn’t the expected usage.

Leave a Comment