I'm using C#/.NET to deserialize a XML file that looks akin to this:
<?xml version="1.0" encoding="utf-8" ?>
<Books>
<Book
Title="Animal Farm"
>
<Thing1>""</Thing1>
<Thing2>""</Thing2>
<Thing3>""</Thing3>
...
<ThingN>""</ThingN>
</Book>
... More Book nodes ...
</Books>
My classes, for the deserialized XML, look like:
[XmlRoot("Books")]
public class BookList
{
// Other code removed for compactness.
[XmlElement("Book")]
public List<Book> Books { get; set; }
}
public class Book
{
// Other code removed for compactness.
[XmlAttribute("Title")]
public string Title { get; set; }
[XmlAnyElement()]
public List<XmlElement> ThingElements { get; set; }
public List<Thing> Things { get; set; }
}
public class Thing
{
public string Name { get; set; }
public string Value { get; set; }
}
When deserializing, I want all the child nodes of the Book element (<Thing1> through <ThingN>) to be deserialized into a Book's Things collection. However, I'm unable to figure out how to accomplish that. Right now, I'm stuck storing the Thing nodes in the ThingElements collection (via XmlAnyElement).
Is there a way to deserialize heterogeneous child nodes into a collection (of non-XmlElements)?