XML Serialization of derived classes
-
Hi I wanted to ask if it is possible to do something like this and how to do it: I would like to have a xml structure like this:
<root>
<collection>
<element />
<element attribute="value" />
<element />
</collection>
<something />
<somethingother />
</root>The elements in the collection should be of the type ElementBase if the attribute is not set or of the type ElementDerived if the attribute is set to a value (the second element). So the Deserialized collection should be something like: List<ElementBase> collection = new List<ElementBase>() { new ElementBase(), new ElementDerived(), new ElementBase() }; ElementDerived would be derived from ElementBase:
class ElementDerived : ElementBase {}
I hope my question is described clearly. Thanks for your help :rose: Roland
Hi! I'm a signature virus. Copy me into your sig file and help me spread!
-
Hi I wanted to ask if it is possible to do something like this and how to do it: I would like to have a xml structure like this:
<root>
<collection>
<element />
<element attribute="value" />
<element />
</collection>
<something />
<somethingother />
</root>The elements in the collection should be of the type ElementBase if the attribute is not set or of the type ElementDerived if the attribute is set to a value (the second element). So the Deserialized collection should be something like: List<ElementBase> collection = new List<ElementBase>() { new ElementBase(), new ElementDerived(), new ElementBase() }; ElementDerived would be derived from ElementBase:
class ElementDerived : ElementBase {}
I hope my question is described clearly. Thanks for your help :rose: Roland
Hi! I'm a signature virus. Copy me into your sig file and help me spread!
-
Hi I wanted to ask if it is possible to do something like this and how to do it: I would like to have a xml structure like this:
<root>
<collection>
<element />
<element attribute="value" />
<element />
</collection>
<something />
<somethingother />
</root>The elements in the collection should be of the type ElementBase if the attribute is not set or of the type ElementDerived if the attribute is set to a value (the second element). So the Deserialized collection should be something like: List<ElementBase> collection = new List<ElementBase>() { new ElementBase(), new ElementDerived(), new ElementBase() }; ElementDerived would be derived from ElementBase:
class ElementDerived : ElementBase {}
I hope my question is described clearly. Thanks for your help :rose: Roland
Hi! I'm a signature virus. Copy me into your sig file and help me spread!
This is possible if you write your own custom xml deserializer. Based on your xml rules build the deserialization rules and then, at the parsing of element nodes, check if there is an atribute defined, and based on the result instantiate your element class. But I don't understand why you want to use something like this (inheritance), why not just put a flag in the base class (a bool flag, or an enum flag) to diferentiate between the two element types?
I have no smart signature yet...