I need to import a XML file looking like this:
<Carpark>
<Trucks>
<Truck brand='Chrysler' kg='2100'/>
</Trucks>
<Cars>
<Car brand='Mercedes' kg='1100'/>
</Cars>
<Tractors>
<Tractor brand='John Deere' kg='1500'/>
</Tractors>
</Carpark>
Essentially I want to have a list of Carpark
like this:
public class Carpark
{
// Type can be for example Truck, Car
public string Type { get; set; }
public string Brand { get; set; }
public int Weight { get; set; }
}
I don't understand how to convert this XML file to the class I want.
What I have tried is:
XElement xElement = XElement.Load(filePath);
IEnumerable<Carpark> carpark = xElement.Elements();
foreach(var vehicle in carpark)
{
// Once I wrote out the line I was confused as to how to proceed
Console.WriteLine(vehicle.ToString());
}