gpt4 book ai didi

.net - DateTime 和 xsd :date? 的往返 XML 序列化

转载 作者:行者123 更新时间:2023-12-04 01:06:38 25 4
gpt4 key购买 nike

好的,我在这里错过了什么? MSDN 关于 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.



然而:
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();
}
}
}

因此,对于定义为“date”而不是“dateTime”的 XSD 元素,日期不会序列化为 UTC。这是一个问题,因为如果我反序列化这个 XML,生成的日期将是未指定的类型,并且任何转换为​​ UTC(这实际上应该是一个空操作,因为日期的 UTC-ness 应该在往返期间保留),至少会改变一天中的时间,有 50% 的几率是昨天,这取决于您是在格林威治东部还是西部。

日期不应该写成:
  <Date>2009-08-18Z</Date>

?

事实上,如果我反序列化一个包含上述内容的文档,我会得到一个已经转换为本地时间的 DateTime(我在纽约,所以是 8 月 17 日 20:00),如果我立即将该对象序列化回 XML,我得到:
  <Date>2009-08-17</Date>

因此,UTC在进入的途中被转换为Local,并且该Local的时间部分在退出时掉了,这将使其在返回的途中成为Unspecified。我们已经失去了对 8 月 18 日原始 UTC 日期规范的所有了解。

以下是 W3C 关于 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.



根本问题是,如果我执行以下操作:
  • 构造(或以其他方式接收)UTC 日期时间值。
  • 使用将该字段定义为 xsd:date
  • 的架构序列化为 XML
  • 将该 XML 反序列化回 DateTime。
  • 将 DateTime 转换为 UTC(这应该没有影响,因为“往返”应该保留了这一点)。

  • 或以下内容:
  • 反序列化包含 UTC xsd:date 对象(例如 2009-08-18Z)的 XML 文档。
  • 将其序列化回一个新的 XML 文档,而无需对其进行修改。

  • 这些程序中的任何一个都应该让我得到我输入的相同日期。

    解决方法

    到目前为止我能看到的唯一方法是获得 往返我期望的行为是实现 Date 属性如下,假设所有 xsd:date 元素都表示 UTC:
    [XmlElement(DataType = "date")]
    public DateTime Date {
    get { return _dt; }
    set { _dt = value.Kind == DateTimeKind.Unspecified ?
    DateTime.SpecifyKind(value, DateTimeKind.Utc) :
    value.ToUniversalTime(); }
    }

    最佳答案

    我打开了一个 Connect 问题,并从 Microsoft 那里得到了这个,证实了我的担忧:

    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.

    关于.net - DateTime 和 xsd :date? 的往返 XML 序列化,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/1297506/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com