在没有毫秒和 gmt 的情况下将 DateTime 序列化为时间

Serializing DateTime to time without milliseconds and gmt(在没有毫秒和 gmt 的情况下将 DateTime 序列化为时间)
本文介绍了在没有毫秒和 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 序列化为时间的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本站部分内容来源互联网,如果有图片或者内容侵犯您的权益请联系我们删除!

相关文档推荐

c# Generic Setlt;Tgt; implementation to access objects by type(按类型访问对象的C#泛型集实现)
InvalidOperationException When using Context Injection in ASP.Net Core(在ASP.NET核心中使用上下文注入时发生InvalidOperationException)
quot;Overflowquot; compiler error with -9223372036854775808L(编译器错误-9223372036854775808L(Q;溢出Q))
Visual Studio 2010 ReportViewer Assembly References(Visual Studio 2010 ReportViewer程序集引用)
Weird behaviour when I open a reportviewer in WPF(在WPF中打开报表查看器时出现奇怪的行为)
how do i pass parameters to aspnet reportviewer(如何将参数传递给aspnet report查看器)