I have an abstract class with several specific implementations. This is necessary for serialization in XML to be sent to another system - this works fine. However, I also need to be able to deserialize the same XML structure. No matter what I try, I seem to be unable to do this. My class structure is as follows:
Abstract class:
[XmlIncludeAttribute(typeof(ConcreteFooOne))] [XmlIncludeAttribute(typeof(ConcreteFooTwo))] [XmlIncludeAttribute(typeof(ConcreteFooThree))] [XmlRoot(ElementName = "FooData", Namespace="http://foo.bar")] public abstract partial class AbstractFoo {
Concrete class example:
public partial class ConcreteFooOne : AbstractFoo {
XML root example:
<FooData xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:type="ConcreteFooOne" RequestResponse="Request" xmlns="http://foo.bar">
An example is the XML root example, which seems to relate to the problem. Now I can serialize fine, but when deserializing, if I deserialize by going into an abstract type, I certainly get an exception stating that the type "AbstractFoo" is abstract. So I just changed the logic so that a concrete type (ConcreteFooOne in this case) is passed to the serializer instead. Now I get βhttp://foo.bar"> not expected. "I assume this is because the serializer does not know what the root node needs?
I have a node root defined in an abstract class, since it will be the same for all concrete concrete implementations. The specific type is determined by the "RequestResponse" attribute (or the xsi: type attribute may also work if present, as this gives us the actual type name). Is there a way to get the serializer to pick up what is required from an abstract class, or am I completely wrong about that?
- Please note that I cannot change the structure of the class too much, as it is very closely related to some XML schemes provided by a third party.
Thanks in advance for helping with this, it would be very grateful.
source share