简体   繁体   中英

XML serialization without null XMLArray

Let me take it straight. Is it possible to not serialize an XMLArray elements if it null? As in following XML "Details" is used. Is it possible to not to have it in XML if it is null. Please check my code and will appreciate some thoughts on it.

<agr:InvoiceNo>99999</agr:InvoiceNo>
    <agr:Header>
      <agr:LineNo>1</agr:LineNo>
      <agrlib:InvoiceDate>2013-02-13</agrlib:InvoiceDate>
      <agrlib:DueDate>2013-03-15</agrlib:DueDate>
      <agr:ArchiveRef>27624642</agr:ArchiveRef>
      <agr:ArchivePath>Images\20130315\10_00000030_00000</agr:ArchivePath>
      <agr:Currency>SEK</agr:Currency>
      <agr:Seller>
        <agrlib:CompRegNo>999999</agrlib:CompRegNo>
      </agr:Seller>
      <agr:Buyer>
        <agrlib:CompanyCode>10</agrlib:CompanyCode>
        <agr:Accountable />
      </agr:Buyer>
      <agr:PaymentInfo>
        <agr:AccountNumber>99999</agr:AccountNumber>
        <agrlib:BacsId />
      </agr:PaymentInfo>
      <agrlib:ReferenceCode>
        <agrlib:Code>AA</agrlib:Code>
        <agrlib:Value>AAAA</agrlib:Value>
        <agrlib:Description />
      </agrlib:ReferenceCode>
    </agr:Header>
    <Details />                    <!-- this one -->
    <agr:Summary>
      <agr:TotalTax>170.36</agr:TotalTax>
      <agr:TotalInclTax>1590.00</agr:TotalInclTax>
    </agr:Summary>
  </agr:Invoice>

[Serializable]
public class Invoice
{
    private Header _header = new Header();
    private Summary _summary = new Summary();
    private List<Detail> _details = new List<Detail>();

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

    [XmlElement("Header")]
    public Header Header
    {
        get { return _header; }
        set { _header = value; }
    }

    [XmlArray("Details"), XmlArrayItem("Detail", typeof(Detail), IsNullable=false)]
    public List<Detail> Details
    {
        get { return _details; }
        set { _details = value; }
    }

    [XmlElement("Summary")]
    public Summary Summary
    {
        get { return _summary; }
        set { _summary = value; }
    }
}

[XmlType(TypeName = "Detail"), Serializable]
public class Detail
{
    private Product _product = new Product();
    static CultureInfo ci = CultureInfo.InvariantCulture;
    private float _lineTotExclTax = 0;

    [XmlElement("LineNo")]
    public int LineNo { get; set; }

    [XmlIgnore]
    public float LineTotExclTax
    {
        get { return _lineTotExclTax; }
        set { _lineTotExclTax = value; }
    }

    [XmlElement("LineTotExclTax")]
    public string CustomLineTotExclTax
    {
        get { return LineTotExclTax.ToString("#0.00", ci); } 
        set { float.TryParse(value, NumberStyles.Float, ci, out _lineTotExclTax); }
    }

    [XmlElement("Products")]
    public Product Product
    {
        get { return _product; }
        set { _product = value; }
    }
}

If the list truly is null it is ignored. To get the output you show, it must be an empty non- null list, probably due to the field-initializer:

private List<Detail> _details = new List<Detail>();

If you can't make it null , then consider ShouldSerialize* :

[Browsable(false), EditorBrowsable(EditorBrowsableState.Never)]
public bool ShouldSerializeDetails()
{
    return _details != null && _details.Count != 0;
}

This defines a custom rule for when the property Details should / should not be serialized.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM