Inlining XML elements

21 Views Asked by At

I have the following models:

public PersonInfo
{
    [XmlElement("initials")]
    public string Initials { get; set; }

    [XmlElement("prefix")]
    public string Prefix { get; set; }

    [XmlElement("familyName")]
    public string FamilyName { get; set; }
}
public ContactPerson
{
    [XmlElement("department")]
    public string Department { get; set; }

    public PersonInfo PersonInfo { get; set; }
}
public Driver
{
    [XmlElement("vehicleID")]
    public string VehicleId { get; set; }

    public PersonInfo PersonInfo { get; set; }
}

Is there a way to inline the properties of PersonInfo when serializing with XmlSerializer so that I get

<contactPerson>
  <department>SillyWalks</department>
  <initials>J</initials>
  <familyName>Time</familyName>
</contactPerson>

instead of

<contactPerson>
  <department>SillyWalks</department>
  <PersonInfo>
    <initials>J</initials>
    <familyName>Time</familyName>
  </PersonInfo>
</contactPerson>

Inheritance is not an option because I have multiple models like these that needs to be inlined.

0

There are 0 best solutions below