XML 为继承的对象序列化 C#
XML Serialize for inherited objects C#
我有以下 XML 场景:
<al2:Dispatch xmlns:al1="al:1.0.0" xmlns:al2="al:2.0.0">
<al2:MsgIdentificator>0001</al2:MsgIdentificator>
..
<al1:DispatchReceiverGroup>
<al2:Receiver>
<al1:ANumberIdentificator>100001</al1:ANumberIdentificator>
<al1:BNumberIdentificator>1000000001</al1:BNumberIdentificator>
</al2:Receiver>
</al1:DispatchReceiverGroup>
</al2:Dispatch>
所以我的模型如下:
[Serializable]
[XmlRoot(Namespace = "al:2.0.0", ElementName = "Dispatch")]
public class BatchDistribution
{
[XmlElement(Namespace = "al:2.0.0", ElementName = "MsgIdentificator")]
public string MessageIdentificator { get; set; }
//CONFUSED HERE
[XmlArray(Namespace = "al:1.0.0", ElementName = "DispatchReceiverGroup")]
[XmlArrayItem(Namespace = "al:2.0.0", ElementName = "Receiver")]
public List<DistributionReceiver> DistributionRecievers { get; set; }
}
}
所以我想要可选数量的 ANumberIdentificator
和 BNumberIdentificator
元素。为此,我做了一个BaseClassDistributionReceiver
,由DistributionReceiverA
和DistributionReceiverB
继承,如下:
[Serializable]
public class DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0")] //note i dont assign value for Element name because it has to be decided by sub classes
public string NumberIdentificator{ get; set; }
}
和子 类:
[Serializable]
public class DistributionRecieverA : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "ANumberIdentificator")]
public new string ANumberIdentificator { get; set; }
}
和另一个
[Serializable]
public class DistributionRecieverB : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "BNumberIdentificator")]
public new string BNumberIdentificator { get; set; }
}
问题是:我没有序列化 ANumberIdentificator
和 BNumberIdentificator
。
XmlArrayItem 必须枚举数组中所有可能的类型
XmlArrayItem(ElementName="DistributionReceiver", Type=typeof(DistributionReciever)),
XmlArrayItem(ElementName="DistributionRecieverA", Type=typeof(DistributionRecieverA)),
XmlArrayItem(ElementName="DistributionRecieverB", Type=typeof(DistributionRecieverB)),
序列化程序需要结束,从 ElementName 到 Runtime-Type。 (自己添加您的命名空间)。 XmlArrayItem 的位置是正确的,但是添加了多个 !
如果您的 base-class 是抽象的,请不要为其创建 XmlArrayItem。
看起来 ANumberIdentificator
和 BNumberIdentificator
的模型不正确
我已经使用 xmltocsharp 转换示例 XML 模型看起来像
[XmlRoot(ElementName = "Receiver", Namespace = "al:2.0.0")]
public class Receiver
{
[XmlElement(ElementName = "ANumberIdentificator", Namespace = "al:1.0.0")]
public List<string> ANumberIdentificator { get; set; }
[XmlElement(ElementName = "BNumberIdentificator", Namespace = "al:1.0.0")]
public List<string> BNumberIdentificator { get; set; }
}
[XmlRoot(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public class DispatchReceiverGroup
{
[XmlElement(ElementName = "Receiver", Namespace = "al:2.0.0")]
public Receiver Receiver { get; set; }
}
[XmlRoot(ElementName = "Dispatch", Namespace = "al:2.0.0")]
public class Dispatch
{
[XmlElement(ElementName = "MsgIdentificator", Namespace = "al:2.0.0")]
public string MsgIdentificator { get; set; }
[XmlElement(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public DispatchReceiverGroup DispatchReceiverGroup { get; set; }
[XmlAttribute(AttributeName = "al1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al1 { get; set; }
[XmlAttribute(AttributeName = "al2", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al2 { get; set; }
}
我已经使用 XmlSerializer
将 XML 转换为对象
XmlSerializer serializer = new XmlSerializer(typeof(Dispatch));
using (TextReader reader = new StringReader(xmlstring))
{
//convert the xml to object
Dispatch result = (Dispatch)serializer.Deserialize(reader);
}
并且我能够在 Receiver 下读取 A 和 B
Console.WriteLine(result.DispatchReceiverGroup.Receiver.ANumberIdentificator);
Console.WriteLine(result.DispatchReceiverGroup.Receiver.BNumberIdentificator);
我有以下 XML 场景:
<al2:Dispatch xmlns:al1="al:1.0.0" xmlns:al2="al:2.0.0">
<al2:MsgIdentificator>0001</al2:MsgIdentificator>
..
<al1:DispatchReceiverGroup>
<al2:Receiver>
<al1:ANumberIdentificator>100001</al1:ANumberIdentificator>
<al1:BNumberIdentificator>1000000001</al1:BNumberIdentificator>
</al2:Receiver>
</al1:DispatchReceiverGroup>
</al2:Dispatch>
所以我的模型如下:
[Serializable]
[XmlRoot(Namespace = "al:2.0.0", ElementName = "Dispatch")]
public class BatchDistribution
{
[XmlElement(Namespace = "al:2.0.0", ElementName = "MsgIdentificator")]
public string MessageIdentificator { get; set; }
//CONFUSED HERE
[XmlArray(Namespace = "al:1.0.0", ElementName = "DispatchReceiverGroup")]
[XmlArrayItem(Namespace = "al:2.0.0", ElementName = "Receiver")]
public List<DistributionReceiver> DistributionRecievers { get; set; }
}
}
所以我想要可选数量的 ANumberIdentificator
和 BNumberIdentificator
元素。为此,我做了一个BaseClassDistributionReceiver
,由DistributionReceiverA
和DistributionReceiverB
继承,如下:
[Serializable]
public class DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0")] //note i dont assign value for Element name because it has to be decided by sub classes
public string NumberIdentificator{ get; set; }
}
和子 类:
[Serializable]
public class DistributionRecieverA : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "ANumberIdentificator")]
public new string ANumberIdentificator { get; set; }
}
和另一个
[Serializable]
public class DistributionRecieverB : DistributionReceiver
{
[XmlElement(Namespace = "al:1.0.0", ElementName = "BNumberIdentificator")]
public new string BNumberIdentificator { get; set; }
}
问题是:我没有序列化 ANumberIdentificator
和 BNumberIdentificator
。
XmlArrayItem 必须枚举数组中所有可能的类型
XmlArrayItem(ElementName="DistributionReceiver", Type=typeof(DistributionReciever)),
XmlArrayItem(ElementName="DistributionRecieverA", Type=typeof(DistributionRecieverA)),
XmlArrayItem(ElementName="DistributionRecieverB", Type=typeof(DistributionRecieverB)),
序列化程序需要结束,从 ElementName 到 Runtime-Type。 (自己添加您的命名空间)。 XmlArrayItem 的位置是正确的,但是添加了多个 !
如果您的 base-class 是抽象的,请不要为其创建 XmlArrayItem。
看起来 ANumberIdentificator
和 BNumberIdentificator
我已经使用 xmltocsharp 转换示例 XML 模型看起来像
[XmlRoot(ElementName = "Receiver", Namespace = "al:2.0.0")]
public class Receiver
{
[XmlElement(ElementName = "ANumberIdentificator", Namespace = "al:1.0.0")]
public List<string> ANumberIdentificator { get; set; }
[XmlElement(ElementName = "BNumberIdentificator", Namespace = "al:1.0.0")]
public List<string> BNumberIdentificator { get; set; }
}
[XmlRoot(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public class DispatchReceiverGroup
{
[XmlElement(ElementName = "Receiver", Namespace = "al:2.0.0")]
public Receiver Receiver { get; set; }
}
[XmlRoot(ElementName = "Dispatch", Namespace = "al:2.0.0")]
public class Dispatch
{
[XmlElement(ElementName = "MsgIdentificator", Namespace = "al:2.0.0")]
public string MsgIdentificator { get; set; }
[XmlElement(ElementName = "DispatchReceiverGroup", Namespace = "al:1.0.0")]
public DispatchReceiverGroup DispatchReceiverGroup { get; set; }
[XmlAttribute(AttributeName = "al1", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al1 { get; set; }
[XmlAttribute(AttributeName = "al2", Namespace = "http://www.w3.org/2000/xmlns/")]
public string Al2 { get; set; }
}
我已经使用 XmlSerializer
将 XML 转换为对象
XmlSerializer serializer = new XmlSerializer(typeof(Dispatch));
using (TextReader reader = new StringReader(xmlstring))
{
//convert the xml to object
Dispatch result = (Dispatch)serializer.Deserialize(reader);
}
并且我能够在 Receiver 下读取 A 和 B
Console.WriteLine(result.DispatchReceiverGroup.Receiver.ANumberIdentificator);
Console.WriteLine(result.DispatchReceiverGroup.Receiver.BNumberIdentificator);