【问题标题】:Remove time zone designator when serializing a datetime object in C#在 C# 中序列化日期时间对象时删除时区指示符
【发布时间】:2012-09-28 00:50:09
【问题描述】:

我有一个由 Visual Studio 2008 中的 XSD 工具自动生成的类;这个类看起来像这样:

/// <remarks/>
    [System.CodeDom.Compiler.GeneratedCodeAttribute("xsd", "2.0.50727.3038")]
    [System.SerializableAttribute()]
    [System.Diagnostics.DebuggerStepThroughAttribute()]
    [System.ComponentModel.DesignerCategoryAttribute("code")]
    [System.Xml.Serialization.XmlTypeAttribute(AnonymousType = true, Namespace = "http://www.xxx.com")]
    [System.Xml.Serialization.XmlRootAttribute(Namespace = "http://www.xxx.com", IsNullable = false)]
    public partial class Invoice
    {

        private DateTime startDateField;

      // Some other fields goes here.

             /// <remarks/>
        [System.Xml.Serialization.XmlAttributeAttribute()]
        public System.DateTime startDate
        {
            get
            {
                return this. startDateField;
            }
            set
            {
                this. startDateField = value;
            }
        }
   }

当我序列化一个 Invoice 实例时,我得到一个如下所示的 xml:

<?xml version="1.0" encoding="utf-8"?>
<Invoice xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.xxx.com http://www.xxx/cfdv22.xsd"  startDate ="2012-09-27T16:41:36-07:00">
… some other elements goes here.
</Invoice>

如果你看到属性 startDate 的值包含在末尾的时区。

在实现过程中是否有任何方法可以将日期时间对象转换为其表示形式,例如 yyyy-MM-ddTHH:mm:ss?或者删除时区的东西?

提前致谢。

【问题讨论】:

  • 为什么????至少制作看起来不像精确时间日期的样本比大约在当天发生的时间更有意义。用“AlarmClock.TimeToWakup”替换你的样本怎么样...

标签: c# visual-studio-2008 xml-serialization


【解决方案1】:

XML 中的“日期时间”类型是 ISO8601 日期,根据 XML Schema Recomendation

如果您不关心时区,那么"date" datatype 可能就是您想要的。

【讨论】:

  • 没有。还需要时间。不希望时区偏移。
猜你喜欢
  • 1970-01-01
  • 2012-06-12
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2011-12-16
  • 1970-01-01
  • 2020-01-23
  • 1970-01-01
相关资源
最近更新 更多