【发布时间】:2011-09-13 09:57:32
【问题描述】:
我目前正在编写一个特殊的客户端应用程序,以允许我们的单元测试使用用于原子提要的 XML 结构的 OData 接口。 一切似乎都正常工作,但是当我需要将 DateTime 值作为属性传递时,我遇到了麻烦。
我编写了以下代码,从对象的属性中提取 DateTime 值并将其以特定格式存储:
private static void GenerateProperty<T>(StringBuilder xml, T obj, PropertyInfo info)
{
// Extract the information about the property if it contains a value.
if (info.GetValue(obj, null) == null) return;
string type = info.GetGetMethod().ReturnType.ToString().Split('.').Last();
string value = info.GetValue(obj, null).ToString();
if (type == "DateTime")
value = ((DateTime)info.GetValue(obj, null)).ToString("yyyy-mm-ddThh:mm:ss");
if (type == "Boolean") value = value.ToLower();
// Append the property to the generated XML.
xml.Append(type.ToLower().Equals("string") ?
string.Format("<d:{0}>{1}</d:{0}>", info.Name, value) :
string.Format("<d:{0} m:type=\"Edm.{1}\">{2}</d:{0}>", info.Name, type, value));
}
代码的反射很重,但这不是重点。此代码返回的 DateTime 值采用以下格式:2011-49-13T11:49:41Z
但是,我从我的 OData 服务收到以下错误:
错误处理请求 溪流。从请求有效负载转换值时遇到错误 对于属性“Created”,键入“System.DateTime”,即 属性的预期类型。有关更多信息,请参见内部异常 细节。 字符串 '2011-49-13T11:49:41Z' 不是有效的 AllXsd 价值。 System.FormatException 在 System.Xml.XmlConvert.ToDateTime(字符串 s, XmlDateTimeSerializationMode dateTimeOption) 在 System.Data.Services.Parsing.WebConvert.StringToPrimitive(字符串文本, 类型目标类型) 在 System.Data.Services.Serializers.PlainXmlDeserializer.ConvertValuesForXml(对象 值、字符串属性名、类型类型ToBeConverted)
显然它不理解 DateTime 格式,但是当我查看此处发布的文档时:http://www.odata.org/developers/protocols/overview#AbstractTypeSystem
我希望它是有效的。有人有这方面的经验吗?
【问题讨论】:
-
您的代码没有输出
ZAFAICT,但您的错误消息似乎暗示您的实际代码是。
标签: c# .net wcf datetime odata