How to use XmlSerializer
to Deserialize
to specific DateOnly
and DateTime
format? I receive <ShipDate>31.10.2022</ShipDate>
from and XML API endpoint and it deserializes to 1/1/0001
Asked
Active
Viewed 373 times
0

Kok How Teh
- 3,298
- 6
- 47
- 85
-
1As far as I know, `XmlSerializer` still doesn't support custom DateTime formats. The workaround is still to use a string surrogate property as shown in [Can you specify format for XmlSerialization of a datetime?](https://stackoverflow.com/q/1118833) and [Force XmlSerializer to serialize DateTime as 'YYYY-MM-DD hh:mm:ss'](https://stackoverflow.com/q/3534525). If you want the standard DateOnly xml format, use `[XmlElement(DataType="date")]` as shown in [this answer](https://stackoverflow.com/a/5025905) by [th2tran](https://stackoverflow.com/users/142303/th2tran). – dbc Oct 30 '22 at 05:22
-
1Date/time types in .NET _have no inherent format._ That won't occur until you use say `.ToString()` at which point you can specify a format. – Oct 30 '22 at 06:53
-
Dotnet runtime github issue for this: https://github.com/dotnet/runtime/issues/56711 – Kirsan Jul 11 '23 at 09:10