簡體   English   中英

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>

所以我的 Model 如下:

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

所以我想要ANumberIdentificatorBNumberIdentificator元素的可選數量。 為此,我做了一個 Base Class DistributionReceiver ,由DistributionReceiverADistributionReceiverB繼承,如下:

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

問題是:我沒有得到序列化ANumberIdentificatorBNumberIdentificator

看起來 model 對於ANumberIdentificatorBNumberIdentificator不正確

我使用xmltocsharp來轉換示例 XML 和 model 看起來像

    [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 轉換為 object

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

XmlArrayItem 必須枚舉數組中所有可能的類型

  XmlArrayItem(ElementName="DistributionReceiver", Type=typeof(DistributionReciever)),   
  XmlArrayItem(ElementName="DistributionRecieverA", Type=typeof(DistributionRecieverA)), 
  XmlArrayItem(ElementName="DistributionRecieverB", Type=typeof(DistributionRecieverB)), 

序列化程序需要總結,從 ElementName 到 Runtime-Type。 (自己添加你的命名空間)。 XmlArrayItem 的位置是正確的,但是要添加多個!

如果您的基類是抽象的,請不要為它創建 XmlArrayItem。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM