C# Standard XmlSerializer and inheritance



Good day everyone!


I have a problem with standard XmlSerializer from C#. In my project I have the next class hierarchy:



[XmlInclude(typeof(B))]
[XmlInclude(typeof(C))]
abstract class A { }

[XmlType("btag")]
class B : A {
[XmlText]
public String Value { get; set; }
}

[XmlType("ctag")]
class C : A {
[XmlText]
public String Value { get; set; }
}

[XmlRoot(ElementName="dtag")]
class D {
[XmlArrayItem(typeof(A))]
public List<A> Items { get; set; }
}


I need to serialize class D in the next XML document:



<dtag>
<ctag>val1</ctag>
<btag>val2</btag>
<ctag>val3</ctag>
</dtag>


But serializer serialize object of D class in the next XML document:



<dtag>
<Items>
<C d3p1:type="ctag">val1</C>
<B d3p1:type="btag">val2</B>
<C d3p1:type="ctag">val3</C>
</Items>
</d>


All class names and other details of implementation was omitted and I can't change this class hierarchy. I can change only attributes and add a few fields in classes above. And now is my question - how I can omit tag Items in output XML and make element names ctag and btag?


No comments:

Post a Comment