0

I have an object with DateTime properties. The values do not include the timezone. The Kind property is Local. I then serialize the object as follows:

var settings = new XmlWriterSettings()
{
    Indent = true,
    OmitXmlDeclaration = true
};

using (var sw = new StringWriter())
using (var xw = XmlWriter.Create(sw, settings))
{
    serializer.Serializer.Serialize(xw, myObj);
    myXml = sw.ToString();
}

The string is serialized and the value of the value of the date properties has been modified during serialization and now the time part has the time zone added/subtracted to it.

My first question is: Does the serializer use the system settings to serialize datetime properties?

And my actual question is: Is there a way to make the serializer ignore the timezone (preferably setting some property on the serializer and not modifying objects)?

Ivan Debono
  • 457
  • 3
  • 14
  • There is no way to customize XmlSerializer's DateTime format, see [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). In both those questions the suggested workaround is to use a surrogate property. – dbc Jul 25 '23 at 17:10

1 Answers1

0

To answer to your first question: Yes, the serializer uses the system settings to serialize DateTime properties if the DateTime.Kind property is set to Local. The serialization process converts the DateTime object to the XML format based on the system's time zone settings.

Answer to the actual question: Unfortunately, there is no built-in property in the XmlSerializer class that allows you to directly control the serialization of DateTime properties with respect to time zones. When serializing DateTime properties with XmlSerializer, the time zone information will be included based on the system's settings, and there is no direct way to override this behavior without modifying the DateTime objects themselves.

If you want to avoid modifying the original objects and ensure that the time zone information is not included during serialization, you may need to consider an alternative approach. One possible solution is to create a custom property for each DateTime property in your object that represents the date and time in a specific format (e.g., "yyyy-MM-dd HH:mm:ss") without the time zone information. Then, mark this custom property with the XmlIgnore attribute, so it will not be serialized. You can use this custom property to store the DateTime value without the time zone information and use the original property for internal use or calculations within your application.

  • As the objects are DTO from client (website) to server and I*m using NewtonSoft and Automapper, maybe I can implement the conversion to `DateKind.Unspecified` there. – Ivan Debono Jul 25 '23 at 17:30
  • I think that it is a good idea that you are researching and trying. That way you are going to get a better understanding. I also think that your solution may work ~ thank you for giving it a try. –  Jul 25 '23 at 17:39