[英]C# XML Serializing with Abstract Class
I'm currently trying to set up some commands as classes for serializing for a communication protocol. 我目前正在尝试将一些命令设置为用于序列化通信协议的类。 My code is basically as follows: 我的代码基本如下:
[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(); }
}
}
What I need to get from this is: 我需要从中得到的是:
<Message ver="1" msid="00000000-0000-0000-0000-000000000000">
<Commands>
<Command type="healthcheck" />
<Command type="test" />
</Commands>
</Message>
What I'm getting is: 我得到的是:
<Message ver="1" msid="00000000-0000-0000-0000-000000000000">
<Commands>
<HealthCheckCommand type="healthcheck" />
<TestCommand type="test" />
</Commands>
</CTM>
When I try to override the XmlArrayItem name with the same name, it of course throws an error. 当我尝试使用相同的名称覆盖XmlArrayItem名称时,它当然会引发错误。 If I use a list, then it works, but I get all the namespace stuff inside of the subtypes, which I don't want. 如果我使用列表,那么它可以工作,但我得到了子类型中的所有命名空间内容,这是我不想要的。 I could go in and remove those namespace items after the fact, but I'd rather not. 我可以在事后删除这些命名空间项目,但我宁愿不这样做。 There must be a way to do this. 必须有办法做到这一点。
Thanks for the help! 谢谢您的帮助!
EDIT: 编辑:
Here's the serialization code: 这是序列化代码:
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);
}
}
}
Include the derived types by adding the relevant XmlInclude
attributes onto Message
: 通过在Message
上添加相关的XmlInclude
属性来包含派生类型:
[XmlInclude(typeof(HealthCheckCommand))]
[XmlInclude(typeof(TestCommand))]
Then specify the element name for your Command[]
array items: 然后指定Command[]
数组项的元素名称:
[XmlArrayItem("Command")]
This creates XML like this, which is probably the same as when you used a List
: 这会像这样创建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>
Unfortunately the xsi:type
attributes are required in order for deserialisation to work (else how would the serialiser know which types to use?). 不幸的是, xsi:type
属性是必需的,以便反序列化工作(否则串行器将如何知道要使用哪些类型?)。 These can easily be removed after the fact, though. 不过,事后可以很容易地删除它们。 Parse the XML with XDocument
and remove them like so: 使用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.