XML序列化嵌套

时间:2015-03-19 11:27:47

标签: c# xmlserializer

我正在做一些XML序列化并尝试获取输出:

<Claim>
   <Source>...</Source>
   <Vehicle>...</Vehicle>
   <ThirdParty>...</ThirdParty>
   <ThirdParty>...</ThirdParty>
   <ThirdParty>...</ThirdParty>
</Claim>

但我的输出是:

  <Claim>
       <Source>...</Source>
       <Vehicle>...</Vehicle>
       <ThirdParty>
          <ThirdParty>...</ThirdParty>
          <ThirdParty>...</ThirdParty>
          <ThirdParty>...</ThirdParty>
       </ThirdParty>
    </Claim>

第三方嵌套在List而不是Claim中,我如何在XML的基础上而不是在另一个ThirdParty节点内部获得第三方。我希望这是有道理的。

我正在尝试序列化的对象:

    public class ThirdParty
    {
        public ThirdPartyDetails Details { get; set; }
        public ThirdPartyVehicle Vehicle { get; set; }
        public ThirdPartyPolicy Policy { get; set; }
        public ThirdPartyPrincipleCompany PrincipleCompany { get; set; } 
    }


public class Claim
{
     public List<ThirdParty> ThirdParty { get; set; }
}

Intialization

ThirdParty ThirdParty1 = new ThirdParty{ Details = ThirdPartyDetails, Policy=ThirdPartyPolicy, PrincipleCompany=ThirdPartyPrinciple, Vehicle=ThirdPartyVehicle};

ThirdParty ThirdParty2 = new ThirdParty{ Details = ThirdPartyDetails, Policy=ThirdPartyPolicy, PrincipleCompany=ThirdPartyPrinciple, Vehicle=ThirdPartyVehicle};

List<ThirdParty> LP = new List<ThirdParty>();

LP.Add(ThirdParty1);
LP.Add(ThirdParty2);


Claim Claim = new Claim { ThirdParty = LP };

序列化

  var subReq = Claim;
            using (StringWriter sww = new Utf8StringWriter())
            {
                XmlWriterSettings xmlWriterSettings = new XmlWriterSettings
                {
                    Indent = true,
                    OmitXmlDeclaration = false,
                    Encoding = Encoding.Unicode
                };

                using (XmlWriter writer = XmlWriter.Create(sww, xmlWriterSettings))
                {
                    xsSubmit.Serialize(writer, subReq);
                    var xml = sww.ToString();
                    PrintOutput(xml);

                    Console.WriteLine(xml);
                }
            }

修改 回应彼得:

  

好的,现在我的输出有问题,我没有使用它   我认为是正确的。所以因为我继承了属性   列表我可以使用List的Add方法:   claim.Add(ThirdParty1); claim.Add(ThirdParty2);现在我的输出是    ...   ......这完全是   忽略的来源和车辆。有什么想法吗?

1 个答案:

答案 0 :(得分:3)

您已声明Claim 有一个的ThirdParty集合,但您希望它序列化,就像Claim 是ThirdParty的集合一样。

要获取Claim ThirdParty集合的方案,请从List<ThirdParty>派生声明并添加所需的属性和方法。

此示例假设您有另一个类Vehicle。

public class Claim : List<ThirdParty> {
  public string Source { get; set; }
  public Vehicle Vehicle { get; set; }
}

Claim是ThirdParty的列表,但也有其他属性和可能的​​其他方法。

然而

那不行。如果你这样做,它会停止序列化所有其他属性,你得到的只是集合中的孩子。

坚持使用合成,但使用[XmlElement]属性标记,如下所示:

public class Claim 
{
  public Source Source { get; set; }
  public Driver Driver { get; set; }
  public Owner Owner { get; set; }
  public Vehicle Vehicle { get; set; }
  public Accident Accident { get; set; }
  public Policy Policy { get; set; }
  public Insurer Insurer { get; set; }
  public Solicitor Solicitor { get; set; }
  [XmlElement]
  public List<ThirdParty> ThirdParty { get; set; } 
}