XML序列化变量

时间:2014-09-03 20:41:59

标签: c# xml-serialization

我有一个名为DocInvoicing的对象代表一个Invoice,这个对象有2个纳税人相关,一个是Issuer TaxPayer,另一个是Receiver TaxPayer,每个TaxPayers基本上是同一个对象,每个纳税人都有1个billto地址和一个shipto地址列表,但当我将其序列化为XML时,我希望得到下一个结构

<DocInvoicing>
<IssuerTaxPayer>
    <BuyingContact />
    <AlternatePartyId />
    <AlternatePartyType>DEFAULT</AlternatePartyType>
    <Gln />
    <CorreoElectronico />
    <RegimenFiscal />
    <IdIssuerTaxPayer>0</IdIssuerTaxPayer>
    <IdTaxPayer>1</IdTaxPayer>
    <DocType>CFDI</DocType>
    <TaxPayerType />
    <Rfc>ALR090701B53</Rfc>
    <Name>ASTILLA LOPEZ  Y RIVERA CONSULTORES  S.A. DE C.V.</Name>
    <BillingAddressIssuer>
        <Gln />
        <IdAddress>1</IdAddress>
        <Street>VALLE DE LAS JACARANDAS</Street>
        <ExternalNumber>25</ExternalNumber>
        <InternalNumber />
        <Block>IZCALLI DEL VALLE</Block>
        <Localidad />
        <Reference />
        <Municipio>TULTITLAN</Municipio>
        <State>ESTADO DE MEXICO</State>
        <Country>MEXICO</Country>
        <ZipCode>54945</ZipCode>
    </BillingAddressIssuer>
    <ShipmentAddressIssuer>
        <Gln />
        <IdAddress>2</IdAddress>
        <Street>CARRETERA LAGO DE GUADALUPE</Street>
        <ExternalNumber>KM 3.5</ExternalNumber>
        <InternalNumber />
        <Block>LAGO DE GUADALUPE</Block>
        <Localidad />
        <Reference>TEC DE MONTERREY 6to PISO SALA 3</Reference>
        <Municipio>ATIZAPAN</Municipio>
        <State>ESTADO DE MEXICO</State>
        <Country>MEXICO</Country>
        <ZipCode>52926</ZipCode>
    </ShipmentAddressIssuer>
</IssuerTaxPayer>
<ReceiverTaxPayer>
    <BuyingContact />
    <AlternatePartyId />
    <AlternatePartyType>DEFAULT</AlternatePartyType>
    <Gln />
    <CorreoElectronico />
    <RegimenFiscal />
    <IdIssuerTaxPayer>1</IdIssuerTaxPayer>
    <IdTaxPayer>1205</IdTaxPayer>
    <DocType>ND</DocType>
    <TaxPayerType>R</TaxPayerType>
    <Rfc>XAXX010101000</Rfc>
    <Name>PUBLICO EN GENERAL</Name>
    <BillingAddressReceiver>
        <Gln />
        <IdAddress>1217</IdAddress>
        <Street>MARGARITA MAZA</Street>
        <ExternalNumber />
        <InternalNumber />
        <Block />
        <Localidad />
        <Reference />
        <Municipio />
        <State />
        <Country>MEXICO</Country>
        <ZipCode />
    </BillingAddressReceiver>
    <ShipmentAddressReceiver>
        <Gln />
        <IdAddress>0</IdAddress>
        <Street />
        <ExternalNumber />
        <InternalNumber />
        <Block />
        <Localidad />
        <Reference />
        <Municipio />
        <State />
        <Country />
        <ZipCode />
    </ShipmentAddressReceiver>
</ReceiverTaxPayer>
</DocInvoicing>

正如您所看到的,地址名称不同,对于shipmentAddress,XML中只有一个地址,但在对象中有不同(当然我们只是第一个)

我们通过在2个类ReTaxPayer和IsTaxPayer

上扩展TaxPayer类来解决它

Class TaxPayer:

[Serializable]
public class TaxPayer
{
    ///.... (Other properties)
    private Address billingAddress = new Address();
    private List<Address> shipmentAddresses = new List<Address>();
    [XmlIgnore]
    public List<Address> ShipmentAddresses
    {
        get
        {
            return this.shipmentAddresses;
        }
        set
        {
            this.shipmentAddresses = value;
        }
    }
    [XmlIgnore]
    public Address BillingAddress
    {
        get
        {
            return this.billingAddress;
        }
        set
        {
            this.billingAddress = value;
        }

    }
}

Class IsTaxPayer:

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

Class ReTaxPayer:

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

}

在对象DocInvoicing(代表发票)上添加此内容

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

}

我不喜欢这个解决方案,因为我们有两个不同的扩展对象,只是用这些名称序列化它们

我想知道是否有任何方法可以避免创建这两个扩展类,只有相同的对象但名称不同(如果有一个数组而不是一个地址,那么发货地址不是很重要它是不可能只有一个),我知道你可以修改直接在定义上面声明的元素名称或属性但是这个,我必须修改节点,基于属性的元素

向你提供宝贵的帮助

0 个答案:

没有答案