gpt4 book ai didi

使用抽象类的 C# XML 序列化

转载 作者:数据小太阳 更新时间:2023-10-29 02:39:48 24 4
gpt4 key购买 nike

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

[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 名称时,它当然会引发错误。如果我使用一个列表,那么它就可以工作,但是我得到了子类型中的所有命名空间,这是我不想要的。我可以在事后进入并删除那些 namespace 项,但我宁愿不这样做。必须有办法做到这一点。

感谢您的帮助!

编辑:

这是序列化代码:

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

最佳答案

通过将相关的 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();

关于使用抽象类的 C# XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32995688/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com