0

I have a class, DocumentMetaData, that I am trying to serialize to XML. It's a public class and all the elements within it are public, but there is one element that does not appear in the XML output but should. (Another is marked with XmlIgnore.) I am trying to find out why this element is skipped.

public class DocumentMetaData
{
  [XmlElement(ElementName="docType")]
  public string DocType { get; set; }

  [XmlElement(ElementName = "companyName")]
  public string CompanyName { get; set; }

  [XmlElement(ElementName = "countryName")]
  public string CountryName { get; set; }

  [XmlElement(ElementName = "clientName")]
  public string ClientName { get; set; }

  [XmlElement(ElementName = "manufacturer")]
  public string Manufacturer { get; set; }

  [XmlIgnore]
  public DateTime ConversionDate { get; set; }

  [XmlElement(ElementName = "conversionDate")]
  public string ConversionDateString
  {
     get { return ConversionDate.ToShortDateString(); }
  }

}

The resulting XML looks like this:

<docMeta>
    <docType>Text</docType>
    <companyName>Text</companyName>
    <countryName>Text</countryName>
    <clientName>Text</clientName>
    <manufacturer>Text</manufacturer>
</docMeta>

If I check the data before the call is made to serialize, all the fields have values within them. Why does the ConversionDateString field not serialize?

Frank Luke
  • 1,342
  • 3
  • 18
  • 33

1 Answers1

5

Your property needs a setter to be serialized. Otherwise how would you deserialize it?

You can parse the string to DateTime and assign to ConversionDate in the setter.

Jakub Lortz
  • 14,616
  • 3
  • 25
  • 39