I'm migrating a library from .net framework 4.5.2 to .net core 2.2. I need to use an XmlIgnore on a DateTime, which worked fine in .net framework, but I can't get it to work in .net core.
So far, I've tried using NonSerialized instead of XmlIgnore which of course didn't work since my DateTime object is not a field. I've also looked into using the DataContractSerializer isntead as well.
[Serializable]
[XmlRoot(ElementName = "DELIVERY")]
public class Delivery
{
[XmlIgnore]
public DateTime DeliveryDate { get; set; }
[XmlElement]
public string DeliveryDateString
{
get
{
return DeliveryDate.ToString("yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture) ?? string.Empty;
}
set
{
DateTime dt;
if (DateTime.TryParseExact(value, "yyyy-MM-dd HH:mm:ss", CultureInfo.InvariantCulture, DateTimeStyles.AssumeUniversal, out dt))
{
DeliveryDate = dt;
}
}
}
}
Error CA2235: Field DeliveryDate is a member of type Delivery which is serializable but is of type System.DateTime which is not serializable