I have a List which is populated with objects of various concrete types which subclass BaseType
I am using the WCF DataContractSerializer
<Children>
<BaseType xmlns:d3p1="http://schemas.datacontract.org/2004/07/Tasks"
i:type="d3p1:ConcreteTypeA"></BaseType>
<BaseType xmlns:d3p1="http://schemas.datacontract.org/2004/07/Tasks"
i:type="d3p1:ConcreteTypeB"></BaseType>
</Children>
Is there any way to get this to generate
<Children>
<ConcreteTypeA/>
<ConcreteTypeB/>
</Children>
?
The real goal is to let users generate some XML to load into memory, and the users are of a skill level that asking them for the original XML is not going to be successful.
DataContractSerializer is not designed to let you control the output. It’s designed to be fast, implicit, and easy to attribute a class with.
What you want is the XmlSerializer. This gives you a lot more control over the XML output.
Note that in my example below I specified a lot of things that could have been inferred from the property names but just to give you the sense that you can override them in the attributes. In fact I think this whole class would serialize just fine if all the attributes were removed and some KnownTypeAttributes were applied but I haven’t tested it. I don’t know if this will give you the exact XML you described (it will create a root element above Children) but hopefully this sets you in the right direction.
Attributes That Control XML Serialization
EDIT: I just tested and it produces something very close to what you were seeking.
…produces… (useless xmlns removed from the top)