当前 XmlSerializer 生成以下结构:
<config>
<BaseType xsi:type="DerivedType1" />
<BaseType xsi:type="DerivedType2" />
</config>
有没有办法让它将类型名称放入节点:
<config>
<DerivedType1 />
<DerivedType2 />
</config>
?
Use the XmlElementAttribute
构造函数重载(string elementName, Type type)
。
它允许您根据成员中找到的实际类型替换元素名称。
如果您有多个派生类型,请将其中几个堆叠在一起。
如果您尝试序列化通用集合,例如List<Base>
可能包含 Derived 实例,然后使用XmlArrayItem
以同样的方式。
以这种方式定义也隐式地使派生类型已知,因此XmlInclude
属性不是必需的
示例定义:
[XmlRoot]
public class Data
{
[XmlElement("Derived1", typeof(Derived1))]
[XmlElement("Derived2", typeof(Derived2))]
public Base foo { get; set; }
[XmlArrayItem("Derived1", typeof(Derived1))]
[XmlArrayItem("Derived2", typeof(Derived2))]
public List<Base> fooList { get; set; }
}
public class Base { ... }
public class Derived1 : Base { ... }
public class Derived2 : Base { ... }
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)