I've got a C# structure of classes with an abstract base class and various classes derived from this abstract class.
[System.Xml.Serialization.XmlInclude(typeof(B))]
public abstract class A {
...
}
[Serializable]
[XmlType("typename")]
public class B : A {
...
}
Furthermore, I'm using an instance of class B within another class C as a field with its abstract type like this:
public class C {
...
public A myItem { get; set; } //A is actually of type B
...
}
Now, when I serialize my class C via the standard xmlserializer, I get a XML structure like this:
<C>
<myItem p2:type="typename" xmlns:p2="...">
... //Fields as elements and stuff
</myItem>
</C>
But thats not what I need because I send those serialized C objects to a REST Webservice (which has no valid schema yet). What I actually need is something like this:
<C>
<typename>
... //Fields as elements and stuff
</typename>
</C>
But as you can see above, the xmlserializer seems to prefer the instance field name over the type name set via XmlType. Also, obviously I can't just use XmlElement("typename") for my field in C, because I don't know which concrete implementation of my abstract class A the field will contain.
Has anyone ever had a similar problem and could provide me with a solution to this? Or do i really need to implement IXmlSerializable in my class A and thus within all of my concrete A-derived classes to get this working?
EDIT: Just found out while reading some articles that IXmlSerializable doesn't let me control the wrapper element, so do I actually need to implement the Interface in class C with some sort of switch() on the type of the myItem member?
Thanks for your help!
Best regards, flo