3

I have a hierarchy of classes that are serialised to XML using XMLSerialiser. To do this I am declaring all the concrete types with [XmlInclude]. eg.

 [XmlInclude(typeof(Derived))]
 public class Base
 {
 }

 public class Derived : Base
 {
 }

An instance of Derived gets serialised as:

<Base xsi:type="Derived" />

Is there any way change the type text to something other than the class name? eg:

<Base xsi:type="Fred" />
GazTheDestroyer
  • 20,722
  • 9
  • 70
  • 103

2 Answers2

2

I think you do it as follows:

[XmlType(TypeName = "Fred")]
public class Derived : Base
{
}
Daniel Moses
  • 5,872
  • 26
  • 39
1

Use the XmlType attribute:

[XmlInclude(typeof(Derived))]
public class Base
{
}

[XmlType("Fred")]
public class Derived : Base
{
}

This will give you the desired xsi:type when serializing a Derived object using a Base serializer. My test program output:

<Base xsi:type="Fred"/>
Philip Hanson
  • 1,847
  • 2
  • 15
  • 24