XmlSerializer won’t serialize IEnumerable

The way you serialize an IEnumerable property is with a surrogate property

public class Entity {
   public IEnumerable<Foo> Foo { get; set; }

   [XmlElement, Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
   public List<Foo> FooSurrogate { get { return Foo.ToList(); } set { Foo = value; } }

It’s ugly, but it gets the job done. The nicer solution is to write a surrogate class (i.e. EntitySurrogate).

Leave a Comment