c# – 在OData中提供DateTime值

我正在编写一个特殊的客户端应用程序,以允许我们的单元测试与使用原子馈送的 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}>",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

我希望它有效.任何人都有这方面的经验?

解决方法

YYYY-MM-DDTHH:MM:SS

应该

YYYY-MM-DDTHH:MM:SSZ

相关文章

在要实现单例模式的类当中添加如下代码:实例化的时候:frmC...
1、如果制作圆角窗体,窗体先继承DOTNETBAR的:public parti...
根据网上资料,自己很粗略的实现了一个winform搜索提示,但是...
近期在做DSOFramer这个控件,打算自己弄一个自定义控件来封装...
今天玩了一把WMI,查询了一下电脑的硬件信息,感觉很多代码都...
最近在研究WinWordControl这个控件,因为上级要求在系统里,...