Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 402433
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 12, 20262026-05-12T17:07:29+00:00 2026-05-12T17:07:29+00:00

OK, what am I missing here? MSDN says the following with regard to DateTimeSerializationMode:

  • 0

OK, what am I missing here? MSDN says the following with regard to DateTimeSerializationMode:

In versions 2.0 and later of the .Net
Framework, with this property set to
RoundtripDateTime objects are examined
to determine whether they are in the
local, UTC or an unspecified time
zone, and are serialized in such a way
that this information is preserved.
This is the default behavior and is
recommended for all new applications
that do not communicate with older
versions of the framework.

However:

namespace ConsoleApplication1 {
    public class DateSerTest {
        [XmlElement(DataType = "date")]
        public DateTime Date { get; set; }
    }

    class Program {
        static void Main(string[] args) {
            DateSerTest d = new DateSerTest { 
                Date = DateTime.SpecifyKind(new DateTime(2009,8,18), DateTimeKind.Utc),
            };
            XmlSerializer ser = new XmlSerializer(typeof(DateSerTest));
            using (FileStream fs = new FileStream("out.xml", FileMode.Create)) {
                ser.Serialize(fs, d);
            }

            // out.xml will contain:
            // <Date>2009-08-18</Date>

            using (FileStream fs = new FileStream("out.xml", FileMode.Open)) {
                DateSerTest d1 = (DateSerTest) ser.Deserialize(fs);
                Console.WriteLine(d1.Date); // yields: 8/18/2009 12:00:00 AM
                Console.WriteLine(d1.Date.Kind); // yields: Unspecified
            }

            // in.xml:
            // <DateSerTest>
            //     <Date>2009-08-18Z</Date>
            // </DateSerTest>

            using (FileStream fs = new FileStream("in.xml", FileMode.Open)) {
                DateSerTest d1 = (DateSerTest) ser.Deserialize(fs);
                Console.WriteLine(d1.Date); // yields: 8/17/2009 8:00:00 PM
                Console.WriteLine(d1.Date.Kind); // yields: Local
                using (FileStream fs1 = new FileStream("out2.xml", FileMode.Create)) {
                    ser.Serialize(fs1, d1);

                    // out2.xml will contain:
                    // <Date>2009-08-17</Date>
                }
            }
            Console.ReadKey();
        }
    }
}

So for XSD elements defined as “date” rather than “dateTime”, the date is not serialized as UTC. This is a problem, because if I deserialize this XML the resulting date will be of Kind Unspecified, and any conversion to UTC (which should in fact be a no-op because the UTC-ness of the date should have been preserved during the roundtrip), will change at least the time of day, with a 50% chance of making the date yesterday, depending on whether you’re east or west of Greenwich.

Shouldn’t the date get written as:

  <Date>2009-08-18Z</Date>

?

Indeed, if I deserialize a document that contains the above, I get a DateTime that’s already been converted to Local time (I’m in New York so that’s Aug 17th 20:00), and if I immediately serialize that object back to XML, I get:

  <Date>2009-08-17</Date>

So, UTC was converted to Local on the way in, and the time part of that Local dropped on the way out, which will make it Unspecified on the way back in again. We’ve lost all knowledge of the original UTC date specification of August 18th.

Here’s what the W3C says about xsd:date:

[Definition:] The ·value space· of
date consists of top-open intervals of
exactly one day in length on the
timelines of dateTime, beginning on
the beginning moment of each day (in
each timezone), i.e. ’00:00:00′, up to
but not including ’24:00:00′ (which is
identical with ’00:00:00′ of the next
day). For nontimezoned values, the
top-open intervals disjointly cover
the nontimezoned timeline, one per
day. For timezoned values, the
intervals begin at every minute and
therefore overlap.

The fundamental problem is that if I do the following:

  1. Construct (or otherwise receive) a UTC DateTime value.
  2. Serialize to XML with a schema defining that field as xsd:date
  3. Deserialize that XML back to a DateTime.
  4. Convert the DateTime to UTC (which should have no effect since the “roundtrip” should have preserved this).

Or the following:

  1. Deserialize an XML document containing a UTC xsd:date object (eg. 2009-08-18Z).
  2. Serialize it back to a new XML document without touching it.

Either of these procedures should get me the same date I put in.

Workaround

The only way I can see so far to get the roundtrip behaviour I expect is to implement the Date property as follows, on the assumption that all xsd:date elements represent UTC:

[XmlElement(DataType = "date")]
public DateTime Date {
    get { return _dt; }
    set { _dt = value.Kind == DateTimeKind.Unspecified ? 
                    DateTime.SpecifyKind(value, DateTimeKind.Utc) : 
                    value.ToUniversalTime(); }
}
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-12T17:07:29+00:00Added an answer on May 12, 2026 at 5:07 pm

    I opened a Connect issue and got this back from Microsoft, confirming my fears:

    We have different behaviors for
    handling Date, Time and DateTime
    values. For DateTime values, if
    XmlDateTimeSerializationMode is not
    Local the information about the kind
    (UTC, Local or Unspecified) is
    preserved. This is also true while
    deserializing. However, for Date and
    Time, they are always serialized out
    with the same format: (yyyy-MM-dd for
    Date and HH:mm:ss.fffffff.zzzzzz for
    Time). So the information about kind
    is lost on serializing and
    deserializing. We are opening a
    documentation bug on our side in order
    to improve the documentation about
    this.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I may be missing something here because I thought this might be really easy
Reading this MSDN article titled Working with ObjectSet (Entity Framework) It shows two examples
I'm writing out some xml from C# using the .net framework's XmlTextWriter. This works
I published a simple ASP.net website referring to this link : http://msdn.microsoft.com/en-us/library/1y1404zt%28v=vs.80%29.aspx I managed
What am I missing here? In the following webpage, it's explained how to write
I am using this code sample from here.. http://msdn.microsoft.com/en-us/data/gg685489 [HttpPost] public ActionResult Edit(int id,
What am I missing here? I want to do a simple call to Select()
What am I missing here? I am using the haml_scaffold generator and the pages
What am I missing here? ViewModel: public class ViewModel { public IDictionary<int, string> Entities
Is there some trick that I'm missing here? I've created a templated control, very

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.