3

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; }
    }
}
1
  • Do you mean "null" ? or just "empty" ? null values usually are ignored Commented Mar 15, 2013 at 8:09

1 Answer 1

5

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.

Sign up to request clarification or add additional context in comments.

1 Comment

You are right, it was due to field initializer. Problem solved. Thanks

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.