I have an old XML data file:
<tag1>
<subtag1>a string</subtag1>
<subtag2>NIL</subtag2>
<subtag3>14/06/2023</subtag3>
</tag1>
<tag1>
<subtag1>another string</subtag1>
<subtag2>12.33</subtag2>
<subtag3>NIL</subtag3>
</tag1>
<tag2/>
...
As can be seen, tag2 is recognized as null-valued/empty-valued by the deserializer, hence the single tag. But the first subtag2 and the second subtag3 in my case is also null, but it is either deserialized as a string ("NIL") or exception thrown runtime.
I have attempted this solution for subtag3 DateOnly
data type:
[XmlIgnore]
public DateOnly? SomeDates { get; set; }
[XmlElement("subtag3")]
public string? SomeDatesString
{
get { return SomeDates ?.ToString("dd/MM/yyyy"); }
set { SomeDates = (value == "NIL" || String.IsNullOrEmpty(value)) ? null : DateOnly.ParseExact(value, "dd/MM/yyyy", CultureInfo.InvariantCulture); }
}
Is there a better, or better yet, optimal best practice solution for recognizing "NIL" as null value?