none
GMT time difference appended in XML fields that are of type datetime2 in the DB. RRS feed

  • Question

  • I am using datetime2 as datatype for all dates in my DB. When I create an XML from the dataset using WriteXML(<xml_file_name>), all the dates appear like they are datetimeoffset ie they all have the GMT time difference attached. So if I now change the timezone on my machine and then ReadXML(<xml_file_name>), all the times are converted to the local time. This may be a desired behavior in some scenarios but not mine. How can I maintain the time that the XML was created with?

    If I manually edit the XML file and remove the GMT time difference addendum, obviously I see teh desired behavior. So is there a way to create the XML file without the GMT time difference appended to my datetimes?

    Monday, February 28, 2011 6:48 PM

Answers

  • Found the solution here:

    http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/b4997b5f-6d68-4858-82cc-967aef603b93

    Basically , you have to change the DataSet.DataTable.DataColumn's DateTimeMode to 'Unspecified' for every column that is of type System.DateTime. Good idea to do this before you load data into the DataSet.

    I created a partial class ( so that the autogenerated code for the DataSets is not overwritten next time I auogenerate) and overloaded the DataSet constructor to go through all the columns in each table. If the type is System.DateTime, then I set the DateTimeMode on that column to 'Unspecified'. This did the trick!!!

    • Marked as answer by srs007 Monday, February 28, 2011 10:14 PM
    Monday, February 28, 2011 10:14 PM

All replies

  • Found the solution here:

    http://social.msdn.microsoft.com/forums/en-US/xmlandnetfx/thread/b4997b5f-6d68-4858-82cc-967aef603b93

    Basically , you have to change the DataSet.DataTable.DataColumn's DateTimeMode to 'Unspecified' for every column that is of type System.DateTime. Good idea to do this before you load data into the DataSet.

    I created a partial class ( so that the autogenerated code for the DataSets is not overwritten next time I auogenerate) and overloaded the DataSet constructor to go through all the columns in each table. If the type is System.DateTime, then I set the DateTimeMode on that column to 'Unspecified'. This did the trick!!!

    • Marked as answer by srs007 Monday, February 28, 2011 10:14 PM
    Monday, February 28, 2011 10:14 PM
  • Hello srs007,

     

    Welcome to the MSDN Forum and thanks for posting here.

    Happy to see that you've got your solution and thank you very much for sharing your solution to us. That's very helpful!

     

    Have a nice day,


    Jackie Sun [MSFT]
    MSDN Community Support | Feedback to us
    Get or Request Code Sample from Microsoft
    Please remember to mark the replies as answers if they help and unmark them if they provide no help.

    Wednesday, March 2, 2011 2:21 AM
    Moderator