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" />
4

2 回答 2

2

我认为你这样做如下:

[XmlType(TypeName = "Fred")]
public class Derived : Base
{
}
于 2012-04-02T15:25:01.407 回答
1

使用XmlType 属性

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

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

这将在使用序列化程序序列化对象xsi:type时为您提供所需的。我的测试程序输出:DerivedBase

<Base xsi:type="Fred"/>
于 2012-04-02T15:29:34.007 回答