0

When you make an object Serializable to XML, if a property marked with the [XmlElement] attribute has a friend with "Specified" on the end of the name (regardless of attributes) like this:

[XmlType(TypeName="XmlAble"),Serializable]
public class XmlAble
{
    [XmlElement]
    public DateTime MyDate { get { return MyDateField; } set { MyDateField = value; MyDateSpecified = true; } }
    private DateTime MyDateField;

    [XmlIgnore]
    public bool MyDateSpecified
    {get;set;}

}

that boolean flag will be used to decide whether the first property (MyDate in my example) gets serialized at all.

Play with this example at .NET Fiddle and you will see what I mean.

Why does this operate in such a clunky way (I consider anything based off of string contains on property names to be very clunky), but has no documentation for it?

NH.
  • 2,240
  • 2
  • 23
  • 37
  • Why not use `DateTime?` to make it nullable, and instead of having a field saying whether or not its specified you infer this by whether it's null or not. – PhonicUK May 08 '18 at 19:19
  • It's documented. See [XML Schema Binding Support: MinOccurs Attribute Binding Support](https://msdn.microsoft.com/en-us/library/zds0b35c.aspx) (official documentation) and [ShouldSerialize*() vs *Specified Conditional Serialization Pattern](https://stackoverflow.com/a/37842985/3744182) (Stackoverflow answer discussing it.) – dbc May 08 '18 at 19:20

0 Answers0