XmlSerializer won’t serialize IEnumerable

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

[XmlRoot]
public class Entity {
   [XmlIgnore]
   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