XML序列化嵌套 [英] XMLSerialization Nesting

查看:45
本文介绍了XML序列化嵌套的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在做一些 XML 序列化并试图获得这样的输出:

I'm doing some XML Serialization and trying to get the output like so:

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

第三方嵌套在列表中而不是声明中,我如何才能在 XML 的基础上而不是在另一个第三方节点中获取第三方.我希望这是有道理的.

The Third Parties are nested inside the List instead of Claim, how can I get the Thirdparties at the base of the XML instead of inside another ThirdParty Node. I hope this makes sense.

我要序列化的对象:

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

初始化

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 然后我可以使用 List 的 Add 方法:索赔.添加(第三方1);索赔.添加(第三方2);现在我的输出是...……完全是忽略来源和车辆.有什么想法吗?

Okay, now I'm having a problem with the output, I'm not using it correctly I think. So because I'm inheriting the properties of List I can then use the Add method of List: claim.Add(ThirdParty1); claim.Add(ThirdParty2); Now my output is ... ... It's completely Ignored Source and Vehicle. Any ideas?

推荐答案

您已声明 Claim 有一个第三方集合,但您希望它序列化,就像 Claim 是一个 第三方集合.

You have declared that Claim has a collection of ThirdParty but you want it serialised as though Claim is a collection of ThirdParty.

要获得其中 Claim 是第三方的 集合的场景,请从 List 派生 Claim,并添加所需的属性和方法.

To obtain a scenario in which Claim is a collection of ThirdParty, derive Claim from List<ThirdParty> and add the required properties and methods.

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

This sample assumes you have another class Vehicle.

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

Claim 是一个第三方列表,但也有其他属性和潜在的其他方法.

Claim is a list of ThirdParty but has other properties and potentially additional methods also.

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

That won't work. If you do that it stops serialising all the other properties, all you get is children of the collection.

所以坚持组合,但是用 [XmlElement] 属性标记它,像这样:

So stick with composition, but mark it up with the [XmlElement] attribute, like this:

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

这篇关于XML序列化嵌套的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆