我目前正在尝试将一些命令设置为用于序列化通信协议的类。我的代码基本如下:
[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);
}
}
}
答案 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();