加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

c# – 在OData中提供DateTime值

发布时间:2020-12-15 06:46:47 所属栏目:百科 来源:网络整理
导读:我正在编写一个特殊的客户端应用程序,以允许我们的单元测试与使用原子馈送的 XML结构的OData接口配合工作. 所有似乎都正常工作,但我遇到麻烦,当我需要传递一个DateTime值作为属性. 我写了以下代码,从对象的属性中提取DateTime值,并以特定的格式存储: privat
我正在编写一个特殊的客户端应用程序,以允许我们的单元测试与使用原子馈送的 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

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读