views:

50

answers:

1

When serializing a custom generic collection to Xml how do I add an attribute to the generated collection element.

Currently I have:

<RootObject>
  <Id>1</Id>
  <Items>
    <MyCollectionItem/>
    <MyCollectionItem/>
  </Items>
</RootObject>

What I need is:

<RootObject>
  <Id>1</Id>
  <Items Name="My collection name">
    <MyCollectionItem/>
    <MyCollectionItem/>
  </Items>
</RootObject>

My code looks like this:

public class RootObject
{
    [XmlArray()]
    public MyCollection Items;

    public string Id;
}

public class MyCollection : Collection<MyCollectionItem>
{
    [XmlAttribute()]
    public string Name;
}
A: 

This is not possible with XML Serialization unless you implement IXmlSerializable on your collection class and implement your own serialization.

John Saunders