title: XMLSerialization Variable
body: <p>I have an object named DocInvoicing representing an Invoice, this object has 2 Taxpayers related, one is an Issuer TaxPayer and the other is a Receiver TaxPayer each of this TaxPayers are basically the same object, every Taxpayer has 1 billto address and a list of shipto addresses, but when I am serializing it to XML I expect to get the next structure</p>
  
  <pre><code>&lt;DocInvoicing&gt;
  &lt;IssuerTaxPayer&gt;
      &lt;BuyingContact /&gt;
      &lt;AlternatePartyId /&gt;
      &lt;AlternatePartyType&gt;DEFAULT&lt;/AlternatePartyType&gt;
      &lt;Gln /&gt;
      &lt;CorreoElectronico /&gt;
      &lt;RegimenFiscal /&gt;
      &lt;IdIssuerTaxPayer&gt;0&lt;/IdIssuerTaxPayer&gt;
      &lt;IdTaxPayer&gt;1&lt;/IdTaxPayer&gt;
      &lt;DocType&gt;CFDI&lt;/DocType&gt;
      &lt;TaxPayerType /&gt;
      &lt;Rfc&gt;ALR090701B53&lt;/Rfc&gt;
      &lt;Name&gt;ASTILLA LOPEZ  Y RIVERA CONSULTORES  S.A. DE C.V.&lt;/Name&gt;
      &lt;BillingAddressIssuer&gt;
          &lt;Gln /&gt;
          &lt;IdAddress&gt;1&lt;/IdAddress&gt;
          &lt;Street&gt;VALLE DE LAS JACARANDAS&lt;/Street&gt;
          &lt;ExternalNumber&gt;25&lt;/ExternalNumber&gt;
          &lt;InternalNumber /&gt;
          &lt;Block&gt;IZCALLI DEL VALLE&lt;/Block&gt;
          &lt;Localidad /&gt;
          &lt;Reference /&gt;
          &lt;Municipio&gt;TULTITLAN&lt;/Municipio&gt;
          &lt;State&gt;ESTADO DE MEXICO&lt;/State&gt;
          &lt;Country&gt;MEXICO&lt;/Country&gt;
          &lt;ZipCode&gt;54945&lt;/ZipCode&gt;
      &lt;/BillingAddressIssuer&gt;
      &lt;ShipmentAddressIssuer&gt;
          &lt;Gln /&gt;
          &lt;IdAddress&gt;2&lt;/IdAddress&gt;
          &lt;Street&gt;CARRETERA LAGO DE GUADALUPE&lt;/Street&gt;
          &lt;ExternalNumber&gt;KM 3.5&lt;/ExternalNumber&gt;
          &lt;InternalNumber /&gt;
          &lt;Block&gt;LAGO DE GUADALUPE&lt;/Block&gt;
          &lt;Localidad /&gt;
          &lt;Reference&gt;TEC DE MONTERREY 6to PISO SALA 3&lt;/Reference&gt;
          &lt;Municipio&gt;ATIZAPAN&lt;/Municipio&gt;
          &lt;State&gt;ESTADO DE MEXICO&lt;/State&gt;
          &lt;Country&gt;MEXICO&lt;/Country&gt;
          &lt;ZipCode&gt;52926&lt;/ZipCode&gt;
      &lt;/ShipmentAddressIssuer&gt;
  &lt;/IssuerTaxPayer&gt;
  &lt;ReceiverTaxPayer&gt;
      &lt;BuyingContact /&gt;
      &lt;AlternatePartyId /&gt;
      &lt;AlternatePartyType&gt;DEFAULT&lt;/AlternatePartyType&gt;
      &lt;Gln /&gt;
      &lt;CorreoElectronico /&gt;
      &lt;RegimenFiscal /&gt;
      &lt;IdIssuerTaxPayer&gt;1&lt;/IdIssuerTaxPayer&gt;
      &lt;IdTaxPayer&gt;1205&lt;/IdTaxPayer&gt;
      &lt;DocType&gt;ND&lt;/DocType&gt;
      &lt;TaxPayerType&gt;R&lt;/TaxPayerType&gt;
      &lt;Rfc&gt;XAXX010101000&lt;/Rfc&gt;
      &lt;Name&gt;PUBLICO EN GENERAL&lt;/Name&gt;
      &lt;BillingAddressReceiver&gt;
          &lt;Gln /&gt;
          &lt;IdAddress&gt;1217&lt;/IdAddress&gt;
          &lt;Street&gt;MARGARITA MAZA&lt;/Street&gt;
          &lt;ExternalNumber /&gt;
          &lt;InternalNumber /&gt;
          &lt;Block /&gt;
          &lt;Localidad /&gt;
          &lt;Reference /&gt;
          &lt;Municipio /&gt;
          &lt;State /&gt;
          &lt;Country&gt;MEXICO&lt;/Country&gt;
          &lt;ZipCode /&gt;
      &lt;/BillingAddressReceiver&gt;
      &lt;ShipmentAddressReceiver&gt;
          &lt;Gln /&gt;
          &lt;IdAddress&gt;0&lt;/IdAddress&gt;
          &lt;Street /&gt;
          &lt;ExternalNumber /&gt;
          &lt;InternalNumber /&gt;
          &lt;Block /&gt;
          &lt;Localidad /&gt;
          &lt;Reference /&gt;
          &lt;Municipio /&gt;
          &lt;State /&gt;
          &lt;Country /&gt;
          &lt;ZipCode /&gt;
      &lt;/ShipmentAddressReceiver&gt;
  &lt;/ReceiverTaxPayer&gt;
  &lt;/DocInvoicing&gt;
  </code></pre>
  
  <p>As you can see the name of the addresses are different and for shipmentAddress there is only one address in XML but in the object there are different (of course we are taking just the first one)</p>
  
  <p>Actuallly we solve it by extending the class TaxPayer on 2 classes ReTaxPayer and IsTaxPayer </p>
  
  <p>Class TaxPayer: </p>
  
  <pre><code>[Serializable]
  public class TaxPayer
  {
      ///.... (Other properties)
      private Address billingAddress = new Address();
      private List&lt;Address&gt; shipmentAddresses = new List&lt;Address&gt;();
      [XmlIgnore]
      public List&lt;Address&gt; ShipmentAddresses
      {
          get
          {
              return this.shipmentAddresses;
          }
          set
          {
              this.shipmentAddresses = value;
          }
      }
      [XmlIgnore]
      public Address BillingAddress
      {
          get
          {
              return this.billingAddress;
          }
          set
          {
              this.billingAddress = value;
          }
  
      }
  }
  </code></pre>
  
  <p>Class IsTaxPayer:</p>
  
  <pre><code>public class IsTaxPayer : TaxPayer
  {
      public Address BillingAddressIssuer
      {
          get { return this.BillingAddress; }
          set { this.BillingAddress = value; }
      }
  
      public Address ShipmentAddressIssuer
      {
  
          get { return this.ShipmentAddresses[0]; }
          set { this.ShipmentAddresses[0] = value; }
      }
  }
  </code></pre>
  
  <p>Class ReTaxPayer:</p>
  
  <pre><code>public class ReTaxPayer : TaxPayer
  {
      public Address BillingAddressReceiver
      {
          get { return this.BillingAddress; }
          set { this.BillingAddress = value; }
      }
  
  
      public Address ShipmentAddressReceiver
      {
          get { return this.ShipmentAddresses[0]; }
          set { this.ShipmentAddresses[0] = value; }
      }
  
  }
  </code></pre>
  
  <p>And adding this on the object DocInvoicing (representing the invoice)</p>
  
  <pre><code>[XmlRoot("DocInvoicing")]
  public class DocInvoices : DocObj
  {
      private IsTaxPayer issuerTaxPayer = new IsTaxPayer();
  
      public IsTaxPayer IssuerTaxPayer
      {
          get
          {
              return this.issuerTaxPayer;
          }
          set
          {
              this.issuerTaxPayer = value;
          }
      }
  
      private ReTaxPayer receiverTaxPayer = new ReTaxPayer();
  
      public ReTaxPayer ReceiverTaxPayer
      {
          get
          {
              return this.receiverTaxPayer;
          }
          set
          {
              this.receiverTaxPayer = value;
          }
      }
  
  }
  </code></pre>
  
  <p>I don´t like this solution because we have 2 different extended objects only to serialize them with those names</p>
  
  <p>I want to know if there is any way to avoid the creation of this 2 extended classes with nothing more than the same objects but different names (the shipment address is not a big deal to have an array instead of just one address if it cannot be possible to just have one), I know you can modify element names or attributes directly declaring above the definition but what about this, that I have to modify nodes, elements based on the properties</p>
  
  <p>Thnks for your valuable help</p>