简体   繁体   English

如何在没有包装元素的情况下对多态数组进行XML序列化

[英]How to XML serialize polymorphic array without wrapping element

Want to serialise my data into this: 想要将我的数据序列化为:

<?xml version="1.0" encoding="ibm850"?>
<Batch Name="Test batch">
   <ExecuteCommand Command="..." />
   <WaitCommand Seconds="5" />
</Batch>

But instead I'm getting this (note the wrapping Commands element) 但相反,我得到这个(注意包装命令元素)

<?xml version="1.0" encoding="ibm850"?>
<Batch Name="Test batch">
  <Commands><!-- I want to get rid of thiw wrapper Commands element and just  -->
    <ExecuteCommand Command="..." />
    <WaitCommand Seconds="5" />
  </Commands>
</Batch>

Here's the sample code used to produce this: 以下是用于生成此代码的示例代码:

public class BaseCommand //base class
{
    [XmlAttribute]
    public string Result { get; set; }
}

public class ExecuteCommand : BaseCommand
{
    [XmlAttribute]
    public string Command { get; set; }
}

public class WaitCommand : BaseCommand
{
    [XmlAttribute]
    public int Seconds { get; set; }
}

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

    private List<BaseCommand> _commands = new List<BaseCommand>();
    [XmlArrayItem(typeof(ExecuteCommand))]
    [XmlArrayItem(typeof(WaitCommand))]
    public List<BaseCommand> Commands
    {
        get
        {
            return _commands;
        }
        set
        {
            _commands = value;
        }
    }

    public static void Main()
    {
        XmlSerializer serializer = new XmlSerializer(typeof(Batch));

        Batch b = new Batch();
        b.Name = "Test batch";
        b.Commands.Add(new ExecuteCommand() { Command = "..." });
        b.Commands.Add(new WaitCommand() { Seconds = 5 });

        serializer.Serialize(Console.Out, b);
        Console.Read();
    }
}

I searched and read heaps of articles on this topic. 我搜索并阅读了关于这个主题的大量文章。 They all seem to provide solution for serializing collections with a single class type (no inheritance used). 它们似乎都提供了使用单个类型序列化集合的解决方案(不使用继承)。 I use inheritance and nothing seems to work. 我使用继承,似乎没有任何工作。 Unfortunately I have to output precise XML document due to legacy support 不幸的是,由于遗留支持,我必须输出精确的XML文档

This was quite some time ago but I eventually figured it out on my own. 这是很久以前的事了,但我最终还是自己想出来了。

The solution was to add [XmlElement] attribute for each of the supported derived types to the collection property 解决方案是将每个受支持的派生类型的[XmlElement]属性添加到集合属性

private List<BaseCommand> _commands = new List<BaseCommand>();
[XmlElement(typeof(ExecuteCommand))]
[XmlElement(typeof(WaitCommand))]
public List<BaseCommand> Commands
{
    get
    {
        return _commands;
    }
    set
    {
        _commands = value;
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM