序列化派生类型-不在数组中

本文关键字:数组 派生 类型 序列化 | 更新日期: 2023-09-27 18:11:57

通过Visual Studio设置设计器(自动生成的代码)使用.NET XmlSerializer,可以像这样序列化派生类型的数组:

[XmlArrayItem(Type = typeof(Square)), XmlArrayItem(Type = typeof(Triangle))]
public Shape[] Shapes;
...
Properties.Settings.Default.Save();

生成类似

的XML
<Shape>
  <Triangle>....</Triangle>
  <Triangle>....</Triangle>
  <Square>....</Square>
</Shape>

但是如果派生类型不在数组(或任何其他集合)中怎么办?

XmlElement代替XmlArrayItem,以下

[XmlElement(Type = typeof(Square)), XmlElement(Type = typeof(Triangle))]
public Shape Window;

工作但产生

<Triangle>....</Triangle>

其中元素名是类型,而不是属性名,如果我添加第二个Shape:

[XmlElement(Type = typeof(Square)), XmlElement(Type = typeof(Triangle))]
public Shape Door;
[XmlElement(Type = typeof(Square)), XmlElement(Type = typeof(Triangle))]
public Shape Window;

序列化失败了——毕竟,在反序列化时它怎么知道哪个XML元素属于哪个属性呢?

是否缺少一个属性?我是否可以在不编写自定义序列化代码的情况下实现此功能?如何?

序列化派生类型-不在数组中

答案是在基类上使用XmlInclude而不是在属性上使用XmlElement:

[XmlInclude(typeof(Triangle))]
[XmlInclude(typeof(Square))]
public abstract class Shape

生成的XML略有不同。对于数组的情况:

<Shapes>
  <Shape xsi:type="Triangle">....</Shape>
  <Shape xsi:type="Triangle">....</Shape>
  <Shape xsi:type="Square">....</Shape>
</Shapes>

,对于标量情况:

<Door xsi:type="Square">....</Door>
<Window xsi:type="Triangle">....</Window>

完美。

在根对象上添加[XmlInclude]属性是一个很好的解决方案,因为它可以处理数据模型中任何地方Square类型的多态属性的所有出现。

然而,这种情况也可以使用[XmlElement]属性来处理,通过XmlElementAttribute.ElementName消除多态元素的歧义:

public class Room
{
    [XmlElement("DoorSquare", Type = typeof(Square)), XmlElement("DoorTriangle", Type = typeof(Triangle))]  
    public Shape Door { get; set; }
    [XmlElement("WindowSquare", Type = typeof(Square)), XmlElement("WindowTriangle", Type = typeof(Triangle))]  
    public Shape Window { get; set; }
}

生成以下XML:

<Room>
  <DoorTriangle />
  <WindowSquare />
</Room>

样本小提琴。

复合形状序列化初稿:

  [Serializable()]
  [XmlRoot("shape", Namespace = "", IsNullable = false)]
  public abstract class Shape {
    public abstract void Draw();
    [XmlAttribute("name")] public string Name { get; set; }
  }
  [Serializable()]
  [XmlRoot("triangle", Namespace = "", IsNullable = false)]
  public class Triangle : Shape {
    public override void Draw() { }
  }
  [Serializable()]
  [XmlRoot("square", Namespace = "", IsNullable = false)]
  public class Square : Shape {
    public override void Draw() { }
  }
  [Serializable()]
  [XmlRoot("compositeShape", Namespace = "", IsNullable = false)]
  public class CompositeShape : Shape {
    [XmlElement("shape", typeof(Shape))]
    [XmlElement("triangle", typeof(Triangle))]
    [XmlElement("square", typeof(Square))]
    [XmlElement("compositeShape", typeof(CompositeShape))]
    public Shape[] Items { get; set; }
    public override void Draw() { }
  }

使用方式:

var shape = new CompositeShape() {
    Name = "some composite shape",
    Items = new Shape[] {
      new CompositeShape() { 
        Name = "inner composite shape",
        Items = new Shape[] {
          new Triangle() {Name = "level 2 triangle"},
          new Square() {Name="level 2 square"}
        } }, 
        new Triangle() {Name = "level 1 triangle"},
        new Square() {Name="level 1 square"}
    }};
    // serialize ...
样本输出:

<compositeShape name="some composite shape">
  <compositeShape name="inner composite shape">
    <triangle name="level 2 triangle" />
    <square name="level 2 square" />
  </compositeShape>
  <triangle name="level 1 triangle" />
  <square name="level 1 square" />
</compositeShape>

这种方法的缺点是,每次向层次结构中添加对象时,您都必须使用该类型的元素来修饰shape数组,因此不能真正关闭修改…