所有似乎都正常工作,但我遇到麻烦,当我需要传递一个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}>",type,value)); }
代码在反思上是沉重的,但这是旁边的.此代码为DateTime返回的值的格式如下:2011-49-13T11:49:41Z
但是,我从我的OData服务收到以下错误:
Error processing request
stream. Error encountered in converting the value from request payload
for property ‘Created’ to type ‘System.DateTime’,which is the
property’s expected type. See inner exception for more
detail.
The string ‘2011-49-13T11:49:41Z’ is not a valid AllXsd
value.
System.FormatException
at System.Xml.XmlConvert.ToDateTime(String s,
XmlDateTimeSerializationMode dateTimeOption)
at
System.Data.Services.Parsing.WebConvert.StringToPrimitive(String text,
Type targetType)
at
System.Data.Services.Serializers.PlainXmlDeserializer.ConvertValuesForXml(Object
value,String propertyName,Type typeToBeConverted)
所以显然它不明白DateTime格式,但是当我看看这里发布的文档:http://www.odata.org/developers/protocols/overview#AbstractTypeSystem
我希望它有效.任何人都有这方面的经验?
解决方法
@H_301_37@ YYYY-MM-DDTHH:MM:SS应该
YYYY-MM-DDTHH:MM:SSZ