I am trying to deserialize some xml into an IList, but I am having problems. This is what I have done so far:
The XML:
<?xml version="1.0" encoding="utf-8"?>
<Animals>
<Animal>
<Name>Cow</Name>
<Color>Brown</Color>
</Animal>
</Animals>
The Model:
[XmlRoot("Animals")]
public class Model
{
[XmlElement("Animal")]
public IList<Animal> AnimalList { get; set; }
}
public class Animal
{
[XmlElement("Name")]
public string Name{ get; set; }
[XmlElement("Color")]
public string Color{ get; set; }
}
Deserialization:
FileStream fs = new FileStream("file.xml", FileMode.Open);
XmlReader xml = XmlReader.Create(fs);
XmlSerializer ser = new XmlSerializer(typeof(List<Model>));
var list = (List<Model>)ser.Deserialize(xml);
I get an invalid operation exception when running the code above. What am I doing wrong?
Thanks, James Ford
Object
, that is the base class of every class. – Fedora