繁体   English   中英

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

[英]C# XML Serializing with Abstract Class

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

[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);
            }
        }
    }

通过在Message上添加相关的XmlInclude属性来包含派生类型:

[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();

暂无
暂无

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

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