Any way to specify the XML string used in xsi: type = ""?

I have a class hierarchy that is serialized to XML using XMLSerialiser

. For this, I declare all specific types with [XmlInclude]

. eg.

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

 public class Derived : Base
 {
 }

      

The Derived instance is serialized as:

<Base xsi:type="Derived" />

      

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

<Base xsi:type="Fred" />

      

+3


source to share


2 answers


I think you do it like this:



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

      

+2


source


Use the XmlType attribute :

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

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

      



This will give you what you want xsi:type

when serializing the object Derived

with the serializer Base

. The output of my test program is:

<Base xsi:type="Fred"/>

      

+1


source







All Articles