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


基础教程推荐
- JSON.NET 中基于属性的类型解析 2022-01-01
- 首先创建代码,多对多,关联表中的附加字段 2022-01-01
- 从 VS 2017 .NET Core 项目的发布目录中排除文件 2022-01-01
- 错误“此流不支持搜索操作"在 C# 中 2022-01-01
- 全局 ASAX - 获取服务器名称 2022-01-01
- 如何动态获取文本框中datagridview列的总和 2022-01-01
- 是否可以在 asp classic 和 asp.net 之间共享会话状态 2022-01-01
- 在 VS2010 中的 Post Build 事件中将 bin 文件复制到物 2022-01-01
- 将事件 TextChanged 分配给表单中的所有文本框 2022-01-01
- 经典 Asp 中的 ResolveUrl/Url.Content 等效项 2022-01-01