0

Could anyone help me with deserialize and build the model? with this xml:

<event>
    <argument  type="Type1" id="1" name="test1"/>
    <argument  type="Type2" id="1" extra="5"/>
</event>

Event class

[XmlType("event")]
public class Event
{
    [XmlElement("argument")]
    public List<Argument> Arguments { get; set; }
}

Argument class

public abstract class Argument
{
    [XmlAttribute("id")]
    public int Id { get; set; }
}

Type1 class

public class Type1 : Argument
{
    [XmlAttribute("name")]
    public string Name { get; set; }
}

Type2 class

public class Type2 : Argument
{
    [XmlAttribute("extra")]
    public string Extra { get; set; }
}

Inner exception:

{"The specified type is abstract: name='Argument', namespace='', at ."}

Maybe I don't have to use custom deserializer, I just want to deserialize the objects depending on "type" attribute value.

Deserializing way I use:

 public static T XmlDeserializeFromString<T>(this string objectData)
    {
        return (T)XmlDeserializeFromString(objectData, typeof(T));
    }

var obj = xml.XmlDeserializeFromString<Event>();
  • 1
    Possible duplicate of [C# XML serialization of derived classes](http://stackoverflow.com/questions/3326481/c-sharp-xml-serialization-of-derived-classes) – 41686d6564 stands w. Palestine Dec 16 '16 at 08:11
  • 1
    `XmlSerializer` has two mechanisms for handling polymorphism, which are described [here](https://stackoverflow.com/questions/38212559/xml-serialization-of-an-object-initialized-as-array/38217120#38217120). Your `type="Type1"` doesn't follow either. Is your XML format flexible? – dbc Dec 16 '16 at 08:13
  • @dbc sadly it isn't. "Argument" field name is constant etc. – Paweł Charzewski Dec 16 '16 at 08:32

0 Answers0