I've been provided an XML from an API that I can't change, but need to be able to derive the children of a list into its respective classes based on an attribute value.
<someClass>
<list>
<fruit type="banana"></fruit>
<fruit type="orange"></fruit>
<fruit type="apple">
<numWorms>1</numWorms>
</fruit>
</list>
</someClass>
As shown above, an apple
contains a unique element numWorms
that is not present in the other types orange
and banana
.
I would like to parse the XML into a List<Fruit>
, where the derived classes class Apple : Fruit
, class Banana : Fruit
and class Orange : Fruit
are assigned appropriately based on the type
attribute in the XML, so that I can also declare virtual methods and override them for each fruit.
A lot of resources simply recommend changing the XML to be serialised differently which isn't an option as I don't control the API.
What I have so far is this:
...
[XmlElement("list")]
public MyFruitList Fruit { get; set; }
...
...
public class MyFruitList : List<Fruit>, IXmlSerializable
{
public void ReadXml(XmlReader reader)
{
// TODO: This eventually needs to iterate through the entire list
reader.Read();
var type = reader.GetAttribute("type");
Fruit fruit;
switch (type)
{
case "apple":
fruit = new Apple();
break;
default:
fruit = new Fruit(); // For now. More fruit later.
break;
}
this.Add(fruit);
}
}
...
What I'm struggling with from here is whether this is the best/clearest method of achieving what I want, and how to iterate over the rest of the list. A large blocker for me is the inconsistency when debugging; when parsing using this code in 'real-time' it appears to correctly determine the type
of the first element to be banana
, but when I begin debugging and stepping slowly to determine what to do next, the same code evaluates the first element to null
, making it hard to proceed.