0

I want to be able to serialize

class A {
   Base Prop{ get; set; }
}
class B:Base { [XmlText] string Foo {get;set; }
class C:Base { [XmlText] string Bar {get;set; }

to

<A>
  <Prop>
    <B>blah</B>
  </Prop>
</A>

However, the default behavior gives me

<A>
  <Prop xsi:type="B">blah</Prop>
</A>

So basically, I want to discriminate type of the property's value by nesting an element instead of using xsi:type.

Is it possible to do with XML serialization attributes, or do I have to resort to IXmlSerializable?

LOST
  • 2,956
  • 3
  • 25
  • 40
  • If you're willing to just have `blah` without the intermediate `` element, you can add `[XmlElement(typeof(A))] [XmlElement(typeof(B))]` to `Prop` as shown in [this answer](https://stackoverflow.com/a/9497501/3744182) or [this one](https://stackoverflow.com/a/36804496/3744182). – dbc Jul 26 '17 at 07:42
  • @dbc , nah, that would damage readability. – LOST Jul 26 '17 at 11:53

0 Answers0