c# - XmlAttribute 不适用于 XmlArray

标签 c# xml-serialization

我在使用 XmlSerializer 生成以下 XML 结构时遇到问题:

<Root xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
  <Name>This is root.</Name>
  <OtherValue>Otha.</OtherValue>
  <Shapes Name="This attribute is ignored!">
    <Circle>
      <Name>This</Name>
      <Value>Is</Value>
      <Whatever>Circle</Whatever>
    </Circle>
    <Square>
      <Name>And</Name>
      <Value>this is</Value>
      <Something>Square</Something>
    </Square>
  </Shapes>
</Root>

唯一的问题是 <Shapes> 的属性 没有被写入。 我用于序列化的类如下:

public class Root
{
    [XmlElement]
    public string Name { get; set; }

    [XmlElement]
    public string OtherValue { get; set; }

    [XmlArray("Shapes")]
    [XmlArrayItem("Circle", typeof(Circle))]
    [XmlArrayItem("Square", typeof(Square))]
    public ShapeList Shapes { get; set; }
}

public class ShapeList : List<Shape>
{
    // Attribute that is not in output
    [XmlAttribute]
    public string Name { get; set; }
}

public class Shape
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Circle : Shape
{
   public string Whatever { get; set; }
}

public class Square : Shape
{
    public string Something { get; set; }
}

还有一个运行序列化的主要方法:

public static void Main(String[] args)
{
    var extraTypes = new Type[] {
        typeof(Shape),
        typeof(Square),
        typeof(Circle)
    };

    var root = new Root();
    root.Name = "This is root.";
    root.OtherValue = "Otha.";

    root.Shapes = new ShapeList()
    {
        new Circle() { Name = "This", Value="Is", Whatever="Circle" },
        new Square() { Name = "And", Value="this is", Something="Square" }
    };
    root.Shapes.Name = "This is shapes.";

    using (var sw = new StreamWriter("data.xml"))
    {
        var serializer = new XmlSerializer(typeof(Root), extraTypes);
        serializer.Serialize(sw, root);
    }
}
  • 为什么我没有获得 ShapeListName 属性?
  • 如果使用此方法无法完成此任务,是否还有其他简单的方法?

最佳答案

不对数组外部部分进行属性处理;仅处理叶节点 - 集合只是:它们的内容。有一种方法可以做到这一点,如果您不介意使模型变得更复杂一点......

public class Root
{
    [XmlElement]
    public string Name { get; set; }

    [XmlElement]
    public string OtherValue { get; set; }

    [XmlElement("Shapes")]
    public ShapeContainer Shapes { get; set; }
}

public class ShapeContainer
{
    [XmlAttribute]
    public string Name { get; set; }

    private readonly List<Shape> items = new List<Shape>();
    [XmlElement("Circle", typeof(Circle))]
    [XmlElement("Square", typeof(Square))]
    public List<Shape> Items { get { return items; } }
}

public class Shape
{
    public string Name { get; set; }
    public string Value { get; set; }
}

public class Circle : Shape
{
    public string Whatever { get; set; }
}

public class Square : Shape
{
    public string Something { get; set; }
}

用法更改如下:

root.Shapes = new ShapeContainer();
root.Shapes.Items.Add(new Circle() { Name = "This", Value="Is", Whatever="Circle" });
root.Shapes.Items.Add(new Square() { Name = "And", Value = "this is", Something = "Square" });
root.Shapes.Name = "This is shapes.";

关于c# - XmlAttribute 不适用于 XmlArray,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/12466142/

相关文章:

c# - newtonsoft json 序列化时间跨度格式

c# - 我应该始终使用导航属性还是使用 .Where 查询?

c# - 停止 TcpListener 的正确方法

java - 获取 XML 节点位置 - XPath?

java - 使用 apache 公共(public)配置 XMLConfiguration 格式化 XML 输出

c# - 接口(interface)实现和返回类型

c# - 为什么有些人使用 `this.` ?

.net - XMLSrializer 中的 IDeserializationCallback

c# - 使用 System.Xml.Serialization.XmlSerializer 反序列化有效 xml 文件时存在差异

c# - 此 XmlWriter 不支持 base64 编码数据