【发布时间】:2016-03-14 18:06:37
【问题描述】:
我正在修改一组正在序列化的类,但我有一个找不到答案的问题。旧的类有一个非常大的类,称为 Control,它由 ControlType 属性进一步分类
enum ControlType
{
ControlType1 = 0,
ControlType2 = 1
}
public class Control
{
[XmlAttribute("a")]
public string a { get; set; }
[XmlAttribute("b")]
public string b { get; set; }
[XmlAttribute("Type")]
public ControlType Type {get; set;)
}
在上面的简化示例中,原始设计者没有将类分离为子类。我们真正想要的是
class baseControl
{
[XmlAttribute("Type")]
public ControlType Type {get; set;}
}
class Control1 : baseControl
{
[XmlAttribute("a")]
public string a { get; set; }
}
class Control2 : baseControl
{
[XmlAttribute("b")]
public string b { get; set; }
}
我们想分离出类,但我们希望原始 xml 兼容
在旧层次结构中,所有控件类型(由 ControlType 定义)都被序列化为
<Control Type="ControlType1" a="xxxx" />
<Control Type="ControlType2" b="xxxxx" />
如果我们使用新结构显然新结构看起来像
<Control1 Type="ControlType1" a="xxxx" />
<Control2 Type="ControlType2" b="xxxxx" />
但我们确实希望将所有新派生类序列化为“Control”,并且在反序列化时,我们希望根据 Attribute 的值将分配的类型更改为派生类型。
这可能吗?
【问题讨论】:
-
您可以使用
[XmlElement("Control")]确保您的所有控件类都写为<Control ... >。我不确定是否可以使用XmlSerializer确保它们都正确反序列化。
标签: c# xml serialization deserialization