cod*_*nny 10 .net c# wcf datetime odata
我目前正在编写一个特殊的客户端应用程序,以允许我们的单元测试使用原子提要的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));
}
Run Code Online (Sandbox Code Playgroud)
代码很反映,但这不是重点.此代码为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(字符串文本,类型TARGETTYPE)在System.Data.Services.Serializers.PlainXmlDeserializer.ConvertValuesForXml(对象值,String propertyName,类型typeToBeConverted)
显然它不理解DateTime格式,但是当我查看这里发布的文档时:http://www.odata.org/developers/protocols/overview#AbstractTypeSystem
我希望它有效.有人对此有经验吗?
kmc*_*049 21
yyyy-mm-ddThh:mm:ss
应该
yyyy-MM-ddTHH:mm:ssZ
| 归档时间: |
|
| 查看次数: |
14671 次 |
| 最近记录: |