在没有毫秒和 gmt 的情况下将 DateTime 序列化为时间 [英] Serializing DateTime to time without milliseconds and gmt

查看:17
本文介绍了在没有毫秒和 gmt 的情况下将 DateTime 序列化为时间的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用 XSD 文件作为输入创建了一个 C# 类文件.我的一个属性如下所示:

I have created a C# class file by using a XSD-file as an input. One of my properties look like this:

 private System.DateTime timeField;

 [System.Xml.Serialization.XmlElementAttribute(DataType="time")]
 public System.DateTime Time {
     get {
         return this.timeField;
     }
     set {
         this.timeField = value;
     }
 }

序列化后,文件的内容现在如下所示:

When serialized, the contents of the file now looks like this:

<Time>14:04:02.1661975+02:00</Time>

是否有可能,在属性上使用 XmlAttributes,让它在没有毫秒和 GMT 值的情况下呈现?

Is it possible, with XmlAttributes on the property, to have it render without the milliseconds and the GMT-value like this?

<Time>14:04:02</Time>

这可能吗,还是我需要在类被序列化后组合某种 xsl/xpath-replace-magic?

Is this possible, or do i need to hack together some sort of xsl/xpath-replace-magic after the class has been serialized?

这不是将对象更改为字符串的解决方案,因为它在应用程序的其余部分中用作 DateTime,并允许我们使用 XmlSerializer.Serialize() 方法从对象创建 xml 表示.

It is not a solution to changing the object to String, because it is used like a DateTime in the rest of the application and allows us to create an xml-representation from an object by using the XmlSerializer.Serialize() method.

我需要从字段中删除额外信息的原因是接收系统不符合时间数据类型的 w3c 标准.

The reason I need to remove the extra info from the field is that the receiving system does not conform to the w3c-standards for the time datatype.

推荐答案

您可以创建一个字符串属性来执行与 timeField 字段之间的转换,并将序列化属性放在该属性上,而不是其余部分的真实 DateTime 属性应用程序使用.

You could create a string property that does the translation to/from your timeField field and put the serialization attribute on that instead the the real DateTime property that the rest of the application uses.

这篇关于在没有毫秒和 gmt 的情况下将 DateTime 序列化为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆