Serialize a generic collection specifying element names for items in the collection

Posted by mdresser on Stack Overflow See other posts from Stack Overflow or by mdresser
Published on 2010-05-13T11:17:10Z Indexed on 2010/05/13 11:24 UTC
Read the original article Hit count: 270

I have a simple class derived from a generic list of string as follows:

[Serializable]
[System.Xml.Serialization.XmlRoot("TestItems")]
public class TemplateRoleCollection : List<string>
{

}

when I serialize this, I get the following XML:

<TestItems>
  <string>cat</string>
  <string>dog</string>
  <string>wolf</string>
</TestItems>

Is there any way to override the xml element name which is used for serializing items in the collection? I would like the following xml to be produced:

<TestItems>
  <TestItem>cat</TestItem>
  <TestItem>dog</TestItem>
  <TestItem>wolf</TestItem>
</TestItems>

© Stack Overflow or respective owner

Related posts about .NET

Related posts about serialization