使用抽象类进行C#XML序列化

时间:2015-10-07 14:58:57

标签: c# .net xml serialization xmlserializer

我目前正在尝试将一些命令设置为用于序列化通信协议的类。我的代码基本如下:

[XmlRoot("Message")]
[Serializable]
public class Message
{
    private Command[] _commands;

    [XmlAttribute("ver")]
    public int Version { get; set; }
    [XmlAttribute("msid")]
    public Guid Id { get; set; }

    [XmlArray("Commands")]
    [XmlArrayItem(typeof(HealthCheckCommand))]
    [XmlArrayItem(typeof(TestCommand))]
    public Command[] Commands
    {
        get { return _commands; }
        set { _commands = value; }
    }
}

public enum CommandTypes
{
    healthcheck
}

[XmlType(TypeName = "Command")]
public abstract class Command
{
    String CommandType { get; set; }
}

public class HealthCheckCommand : Command
{
    [XmlAttribute("type")]
    public string CommandType
    {
        get { return "healthcheck"; }
        set { throw new NotImplementedException(); }
    }
}

public class TestCommand : Command
{
    [XmlAttribute("type")]
    public string CommandType
    {
        get { return "test"; }
        set { throw new NotImplementedException(); }
    }
}

我需要从中获得:

<Message ver="1" msid="00000000-0000-0000-0000-000000000000">
  <Commands>
    <Command type="healthcheck" />
    <Command type="test" />
  </Commands>
</Message>

我得到的是:

<Message ver="1" msid="00000000-0000-0000-0000-000000000000">
  <Commands>
    <HealthCheckCommand type="healthcheck" />
    <TestCommand type="test" />
  </Commands>
</CTM>

当我尝试使用相同的名称覆盖XmlArrayItem名称时,它当然会引发错误。如果我使用列表,那么它可以工作,但我得到了子类型中的所有命名空间内容,这是我不想要的。我可以在事后删除这些命名空间项目,但我宁愿不这样做。必须有办法做到这一点。

感谢您的帮助!

编辑:

这是序列化代码:

XmlSerializer serializer = new XmlSerializer(typeof (Message));
        using (TextWriter writer = new StreamWriter(@"C:\Xml.txt"))
        {
            XmlSerializerNamespaces xmlSerializerNamespaces = new XmlSerializerNamespaces();
            xmlSerializerNamespaces.Add("", "");
            XmlWriterSettings settings = new XmlWriterSettings();
            settings.OmitXmlDeclaration = true;

            using (
            XmlWriter xmlwriter = XmlWriter.Create(writer, new XmlWriterSettings { OmitXmlDeclaration = true }))
            {
                serializer.Serialize(xmlwriter, message, xmlSerializerNamespaces);
            }
        }
    }

1 个答案:

答案 0 :(得分:1)

通过将相关XmlInclude属性添加到Message

来包含派生类型
[XmlInclude(typeof(HealthCheckCommand))]
[XmlInclude(typeof(TestCommand))]

然后指定Command[]数组项的元素名称:

[XmlArrayItem("Command")]

这会像这样创建XML,这可能与您使用List时的相同:

<Message xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" ver="0" msid="00000000-0000-0000-0000-000000000000">
    <Commands>
        <Command xsi:type="HealthCheckCommand" type="healthcheck" />
        <Command xsi:type="TestCommand" type="test" />
    </Commands>
</Message>

不幸的是,为了使反序列化工作,需要xsi:type属性(否则串行器将如何知道要使用哪些类型?)。不过,事后可以很容易地删除它们。使用XDocument解析XML并将其删除,如下所示:

XNamespace xsi = "http://www.w3.org/2001/XMLSchema-instance";

doc.Descendants()
   .Attributes(xsi + "type")
   .Remove();

doc.Descendants()
   .Attributes()
   .Where(a => a.IsNamespaceDeclaration && a.Value == xsi)
   .Remove();
相关问题