<tfoot id='D9goZ'></tfoot>
      <legend id='D9goZ'><style id='D9goZ'><dir id='D9goZ'><q id='D9goZ'></q></dir></style></legend>
      <i id='D9goZ'><tr id='D9goZ'><dt id='D9goZ'><q id='D9goZ'><span id='D9goZ'><b id='D9goZ'><form id='D9goZ'><ins id='D9goZ'></ins><ul id='D9goZ'></ul><sub id='D9goZ'></sub></form><legend id='D9goZ'></legend><bdo id='D9goZ'><pre id='D9goZ'><center id='D9goZ'></center></pre></bdo></b><th id='D9goZ'></th></span></q></dt></tr></i><div id='D9goZ'><tfoot id='D9goZ'></tfoot><dl id='D9goZ'><fieldset id='D9goZ'></fieldset></dl></div>

      <small id='D9goZ'></small><noframes id='D9goZ'>

        • <bdo id='D9goZ'></bdo><ul id='D9goZ'></ul>

        防止对 DateTime 值的反序列化进行时区转换

        Prevent timezone conversion on deserialization of DateTime value(防止对 DateTime 值的反序列化进行时区转换)

        <small id='zbmV0'></small><noframes id='zbmV0'>

        <tfoot id='zbmV0'></tfoot>

            <bdo id='zbmV0'></bdo><ul id='zbmV0'></ul>

              1. <i id='zbmV0'><tr id='zbmV0'><dt id='zbmV0'><q id='zbmV0'><span id='zbmV0'><b id='zbmV0'><form id='zbmV0'><ins id='zbmV0'></ins><ul id='zbmV0'></ul><sub id='zbmV0'></sub></form><legend id='zbmV0'></legend><bdo id='zbmV0'><pre id='zbmV0'><center id='zbmV0'></center></pre></bdo></b><th id='zbmV0'></th></span></q></dt></tr></i><div id='zbmV0'><tfoot id='zbmV0'></tfoot><dl id='zbmV0'><fieldset id='zbmV0'></fieldset></dl></div>
                  <tbody id='zbmV0'></tbody>
              2. <legend id='zbmV0'><style id='zbmV0'><dir id='zbmV0'><q id='zbmV0'></q></dir></style></legend>

                1. 本文介绍了防止对 DateTime 值的反序列化进行时区转换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                  问题描述

                  我有一个使用 XmlSerializer 序列化/反序列化的类.此类包含 DateTime 字段.

                  I have a class that I serialize/deserialize using XmlSerializer. This class contains a DateTime field.

                  序列化时,DateTime 字段由包含与 GMT 的偏移量的字符串表示,例如 2010-05-05T09:13:45-05:00.反序列化时,这些时间将转换为执行反序列化的机器的本地时间.

                  When serialized, the DateTime field is represented by a string that includes the offset from GMT, e.g 2010-05-05T09:13:45-05:00. When deserialized, these times are converted to the local time of the machine performing the deserialization.

                  出于不值得解释的原因,我想阻止这种时区转换发生.序列化发生在野外,存在这个类的多个版本.反序列化发生在我控制的服务器上.因此,这似乎最好在反序列化期间处理.

                  For reasons not worth explaining, I'd like to prevent this timezone conversion from happening. The serialization happens out in the wild, where multiple version of this class exist. The deserialization happens on a server that's under my control. As such, it seems like this would be best handled during deserialization.

                  除了实现 IXmlSerializable 并手动"完成所有反序列化之外,我怎样才能做到这一点?

                  How can I make this happen, other than implementing IXmlSerializable and doing all of the deserialization "by hand?"

                  推荐答案

                  您可以将其解析为 DateTimeOffset 而不是解析为 DateTime 并使用 DateTimeOffset.DateTime 属性忽略时区.像这样:

                  Instead of parsing as a DateTime you can parse it as a DateTimeOffset and use the DateTimeOffset.DateTime property to ignore the timezone. Like this:

                  [XmlIgnore()]
                  public DateTime Time { get; set; }
                  
                  [XmlElement(ElementName = "Time")]
                  public string XmlTime
                  {
                      get { return XmlConvert.ToString(Time, XmlDateTimeSerializationMode.RoundtripKind); }
                      set { Time = DateTimeOffset.Parse(value).DateTime; }
                  }
                  

                  这篇关于防止对 DateTime 值的反序列化进行时区转换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                  本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                  相关文档推荐

                  Populate ListBox with a IEnumrable on another thread (winforms)(在另一个线程(winforms)上使用 IEnumrable 填充 ListBox)
                  listbox selected item give me quot; System.Data.DataRowViewquot; , C# winforms(列表框选择的项目给我quot;System.Data.DataRowView, C# Winforms)
                  Cannot remove items from ListBox(无法从列表框中删除项目)
                  Preventing ListBox scrolling to top when updated(更新时防止列表框滚动到顶部)
                  Drag and drop from list to canvas on windows phone with MVVM(使用 MVVM 在 Windows 手机上从列表拖放到画布)
                  Deselection on a WPF listbox with extended selection mode(具有扩展选择模式的 WPF 列表框上的取消选择)

                    <tbody id='UeWkB'></tbody>
                2. <i id='UeWkB'><tr id='UeWkB'><dt id='UeWkB'><q id='UeWkB'><span id='UeWkB'><b id='UeWkB'><form id='UeWkB'><ins id='UeWkB'></ins><ul id='UeWkB'></ul><sub id='UeWkB'></sub></form><legend id='UeWkB'></legend><bdo id='UeWkB'><pre id='UeWkB'><center id='UeWkB'></center></pre></bdo></b><th id='UeWkB'></th></span></q></dt></tr></i><div id='UeWkB'><tfoot id='UeWkB'></tfoot><dl id='UeWkB'><fieldset id='UeWkB'></fieldset></dl></div>

                  1. <legend id='UeWkB'><style id='UeWkB'><dir id='UeWkB'><q id='UeWkB'></q></dir></style></legend>
                      <bdo id='UeWkB'></bdo><ul id='UeWkB'></ul>

                        <small id='UeWkB'></small><noframes id='UeWkB'>

                        <tfoot id='UeWkB'></tfoot>