Jez
Jez

Reputation: 29993

Why won't this XML deserialize properly?

Here's an example program showing what I'm trying to do:

http://pastebin.com/m1de1f3ba

The XML in the 'xml' string describes a list of items. The PersonI2 type should be considered as extending the Person type, and therefore I want the XmlSerializer to deserialize the PersonI2 entries in the XML as PersonI2 objects... instead, the XmlSerializer throws an exception. Why, and how can I fix it?

Upvotes: 0

Views: 2089

Answers (1)

Thomas Levesque
Thomas Levesque

Reputation: 292425

Add the XmlInclude attribute to the Person class, to make the XmlSerializer aware of the PersonI2 class :

    [XmlType(AnonymousType = true, TypeName = "Person", Namespace = "")]
    [XmlInclude(typeof(PersonI2))]
    public class Person {
    ...

Upvotes: 2

Related Questions