There's lots of good techniques out there for using the XmlSerializer to serialize a Generic List<> type. You can implement IXmlSerializable and some other trickiness.
Here's the simplest possible thing I could do to make it work while working on a hobby project over lunch today with a friend.
UPDATE: I'm a dork and didn't see the forest for the trees on this one. I don't need a parallel array at all. That's my old 1.1 brain creeping in. Thanks to folks in the comments. It works fine just like this. I'm not sure why I thought it didn't work when I tried it before. Thanks folks!
namespace Poo { public class Foo { public Foo() { FooReadings = new List(); } [XmlArray("FooReadings")] //Even this attribute isn't really needed if you accept the default. public List<FooReading> FooReadings; } public class FooReading { public FooReading() { } public FooReading(DateTime date, decimal thing2) { this.Thing2 = thing2; this.Date = date; } public decimal Thing2; public DateTime Date; } }
The technique below is useful for other things, but not in this instance.
namespace Poo { public class Foo { public Foo() { FooReadings = new List(); } [XmlIgnore] public List<FooReading> FooReadings; [XmlArray("FooReadings")] public FooReading[] ArrayFooReadings { get { return FooReadings.ToArray(); } set { FooReadings = new List(value); } } } public class FooReading { public FooReading() { } public FooReading(DateTime date, decimal thing2) { this.Thing2 = thing2; this.Date = date; } public decimal Thing2; public DateTime Date; } }
The "Real List<>" is ignored and the "fake" one is presented as an Array in the Getters/Setter. Not pretty, to be sure.
What better ways are there that you prefer?
Ads by The Lounge