First, you have a problem with an invalid ending tag - here:
<color>blue<color>
should be:
<color>blue</color>
same with:
<color>white<color>
should be:
<color>white</color>
Next, You need to generate classes for your XML. Here are two options:
Option 1. (Preferred)
Here is a useful tool to generate classes from XML:
[
^]
Generates this:
using System;
using System.Xml.Serialization;
using System.Collections.Generic;
namespace Xml2CSharp
{
[XmlRoot(ElementName="body")]
public class Body {
[XmlElement(ElementName="color")]
public string Color { get; set; }
[XmlElement(ElementName="type")]
public string Type { get; set; }
}
[XmlRoot(ElementName="details")]
public class Details {
[XmlElement(ElementName="year")]
public string Year { get; set; }
[XmlElement(ElementName="make")]
public string Make { get; set; }
}
[XmlRoot(ElementName="cars")]
public class Cars {
[XmlElement(ElementName="body")]
public Body Body { get; set; }
[XmlElement(ElementName="details")]
public Details Details { get; set; }
}
[XmlRoot(ElementName="data")]
public class Data {
[XmlElement(ElementName="cars")]
public List<Cars> Cars { get; set; }
}
}
Option 2.
Also, Visual Studio (tested on VS2017) can also generate classes from XML:
1. Copy XML data to the clipboard
2. In VS, Edit > Paste Special > "Paste Xml as classes"
Generates this:
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
[System.Xml.Serialization.XmlRootAttribute(Namespace = "", IsNullable = false)]
public partial class data
{
private dataCars[] carsField;
[System.Xml.Serialization.XmlElementAttribute("cars")]
public dataCars[] cars
{
get
{
return this.carsField;
}
set
{
this.carsField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCars
{
private dataCarsBody bodyField;
private dataCarsDetails detailsField;
public dataCarsBody body
{
get
{
return this.bodyField;
}
set
{
this.bodyField = value;
}
}
public dataCarsDetails details
{
get
{
return this.detailsField;
}
set
{
this.detailsField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCarsBody
{
private string colorField;
private string typeField;
public string color
{
get
{
return this.colorField;
}
set
{
this.colorField = value;
}
}
public string type
{
get
{
return this.typeField;
}
set
{
this.typeField = value;
}
}
}
[System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true)]
public partial class dataCarsDetails
{
private ushort yearField;
private string makeField;
public ushort year
{
get
{
return this.yearField;
}
set
{
this.yearField = value;
}
}
public string make
{
get
{
return this.makeField;
}
set
{
this.makeField = value;
}
}
}
So for your question,
Option 1. uses for the collection of cars:
[XmlElement(ElementName="cars")]
public List<Cars> Cars { get; set; }
and
Option 2. uses:
private dataCars[] carsField;
[System.Xml.Serialization.XmlElementAttribute("cars")]
public dataCars[] cars
{
get
{
return this.carsField;
}
set
{
this.carsField = value;
}
}
Which could be simplified as:
private dataCars[] carsField;
[System.Xml.Serialization.XmlElement("cars")]
public dataCars[] cars
{
get => carsField;
set => carsField = value;
}