XMLSerialization Nesting

こ雲淡風輕ζ 提交于 2019-12-14 02:45:44

问题


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>

However my output is:

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

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.

My Objects that I'm trying to serialize:

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

Serialization

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

Edit: In response to Peter:

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?


回答1:


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

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

This sample assumes you have another class Vehicle.

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

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

However

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

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


来源:https://stackoverflow.com/questions/29143382/xmlserialization-nesting

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!