Reputation: 6948
I have object of some type, let's say Foo
, looking like:
public class Foo
{
[XmlElement("id")]
public string Id {get; set;}
[XmlElement("name")]
public string Name {get; set;}
}
Also i have xml:
<root>
<foo>
<id>1</id>
<name>name_1</name>
</foo>
<foo>
<id>2</id>
<name>name_2</name>
</foo>
</root>
What am i doing wrong trying to deserialize that xml to List using XmlDeserializer
with following code?
var list = new List<Foo>();
var serializer = new XmlSerializer(typeof(List<Foo>));
using (var reader = new StringReader(xml))
{
list = (List<Foo>)serializer.Deserialize(reader); //error here
}
Getting exception:
System.InvalidOperationException
<root xmlns=''> unexprected .
Upvotes: 0
Views: 142
Reputation: 1062695
The root element does not match. There are ways to pass it into the constructor of XmlSerializer, but IMO your best bet is to create a wrapper class:
[XmlRoot("root")]
public class FooWrapper {
[XmlElement("foo")]
public List<Foo> Items {get;set;}
}
And pass this type to XmlSerializer.
Upvotes: 3