微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

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

原文地址:https://www.jb51.cc/csharp/94669.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐