0

I'm sending data from a winforms project to a Sql Server database by turning the object data into XML and then parsing it on the SQL side out to the target tables.

This works fine, except for an issue with dates.

Often I am not interested in the time part of a datetime value, just the date, so have dates basically entered in the front end as '2021-08-05 00:00:00'.

When I create the XML, it becomes:

2021-08-05T00:00:00+01:00

(+01:00 because I am in the UK, and we are currently an hour ahead of UTC/GMT).

But when that ends up in the Date field in the relevant table, the value has slipped back to 2021-08-04, i.e. yesterday.

Is there a way to control how the XML is generated to stop it adding the time zone difference?

The classes for the data objects are all tagged with [Serializable] at the top. I don't have any special attributes on the date properties themselves.

This is the c# code that converts the object into XML, though I don't think there is a problem here per se (and I probably nicked it from some helpful post up here anyway!):

public static string ConvertToXmlDoc(object dataModeldata)
{
   Type modelType = dataModeldata.GetType();
   var writer = new XmlSerializer(modelType);
   StringWriter sw = new StringWriter();
   writer.Serialize(sw, dataModeldata);
   string xmlDoc = sw.ToString();
   return xmlDoc;
}
dbc
  • 104,963
  • 20
  • 228
  • 340
  • Whats the database data type? Have you tried using a timezone aware data type like `datetimeoffset`? – Nick.Mc Aug 05 '21 at 12:49
  • Hi Nick, it's just date. I don't really want it to be timezone aware -- I just want the value to be that date. Timezone unaware, if you like. Is datetimeoffset a database data type or a C# tpe? Thanks! – Adrian Jones Aug 05 '21 at 12:57
  • 1
    It's a database data type. This timezone stuff can be tricky. If you ever go to Azure SQL, all dates are in UTC timezone. Also it might be difficult to untimezone your XML converter. So you might want to consider attacking it now. If not.... then you probably need to find an XML converter that does not include timeone. i.e. produces a value of `2021-08-05T00:00:00` – Nick.Mc Aug 05 '21 at 13:28
  • What is the value of `yourDate.Kind`? If it is `Local` then it's wrong – Charlieface Aug 05 '21 at 13:30
  • Is every done on same machine? Are the Timezone settings of the machine(s) set correctly? Is c# class that is used for xml serialize a string or a DateTime? – jdweng Aug 05 '21 at 13:37
  • 3
    XML distinguishes between dates (`xsd:date` e.g.: `2021-08-05`) and datetimes (`xsd:dateTime` e.g.: `2021-08-05T00:00:00`). You might consider decorating your Datetime properties with `[XmlElement(DataType="date")]` when they're actually just date values. Ref: [XmlElement](https://learn.microsoft.com/en-us/dotnet/api/system.xml.serialization.xmlelementattribute.datatype) – AlwaysLearning Aug 05 '21 at 14:09
  • @AlwaysLearning that's brilliant and just what I need. Thanks! – Adrian Jones Aug 05 '21 at 15:29
  • So duplicate of [How to serialize Xml Date only from DateTime in C#](https://stackoverflow.com/q/19983302/3744182) then. – dbc Aug 07 '21 at 19:56

0 Answers0