问题描述
为任何时区获取DateTimes。
我使用的是DateTimeOffset,一个字符串和一个XmlElement属性。当我这样做,我得到以下错误:
Working to get DateTimes for any time zone.I'm using DateTimeOffset, and a string, and an XmlElement attribute. When I do, I get the following error:
[InvalidOperationException:There是
一个错误反映类型
'System.String'。]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel
model,String ns,ImportContext
上下文,String dataType,
XmlAttributes a,布尔重复,
布尔openModel,RecursionLimiter
限制器)+1621
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping MemberMapping
访问器,FieldModel模型,
XmlAttributes a,String ns,Type
choiceIdentifierType,Boolean rpc,
Boolean openModel,RecursionLimiter
限制器)+8750
Sys tem.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel
父,FieldModel模型,
XmlAttributes a,String ns,
递归限制器)+139
System.Xml。 Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping
mapping,StructModel model,Boolean
openModel,String typeName,
RecursionLimiter limiter)+1273
[InvalidOperationException: There was an error reflecting type 'System.String'.]
System.Xml.Serialization.XmlReflectionImporter.ImportTypeMapping(TypeModel model, String ns, ImportContext context, String dataType, XmlAttributes a, Boolean repeats, Boolean openModel, RecursionLimiter limiter) +1621
System.Xml.Serialization.XmlReflectionImporter.ImportAccessorMapping(MemberMapping accessor, FieldModel model, XmlAttributes a, String ns, Type choiceIdentifierType, Boolean rpc, Boolean openModel, RecursionLimiter limiter) +8750
System.Xml.Serialization.XmlReflectionImporter.ImportFieldMapping(StructModel parent, FieldModel model, XmlAttributes a, String ns, RecursionLimiter limiter) +139
System.Xml.Serialization.XmlReflectionImporter.InitializeStructMembers(StructMapping mapping, StructModel model, Boolean openModel, String typeName, RecursionLimiter limiter) +1273
[InvalidOperationException:
有一个反映属性
'creationTimeX'的错误。] ...
[InvalidOperationException: There was an error reflecting property 'creationTimeX'.] ...
代码:
[System.Xml.Serialization.XmlElement(ElementName = "creationTime",
DataType="dateTime")]
public string creationTimeX
{
get
{
return this.creationTimeField.ToString("yyyy-MM-ddTHH:mm:sszzz");
}
set
{
DateTimeOffset.TryParse(value, out this.creationTimeField);
}
}
[System.Xml.Serialization.XmlIgnoreAttribute()]
public System.DateTimeOffset creationTime
{
get {
return this.creationTimeField;
}
set {
this.creationTimeField = value;
}
}
推荐答案
看看这个StackOverflow问题关于序列化日期和UTC:
Take a look at this StackOverflow question about serializing dates and UTC:
不需要创建一个特殊的属性来完成序列化。
No need to create a special property just to accomplish the serialization.
这篇关于如何序列化到dateTime的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!