views:

280

answers:

1

Hi,

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?

+1  A: 

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 {
    ...
Thomas Levesque
That doesn't work. You then get an InvalidaOperationException "Cannot include anonymous type 'XmlTester1.PersonI2'".
Chris Arnold
Also, I don't still don't know why this doesn't work but giving a base class (Person) knowledge of an extending class (PersonI2) feels wrong.
Chris Arnold
Actually, this does work if I also then make the AnonymousType's = false. Thanks, Thomas!
Jez