我正在编写一个特殊的客户端应用程序,以允许我们的单元测试使用原子提要的XML结构与OData接口一起工作。 所有似乎都正常工作,但我遇到麻烦时,我需要传递DateTime值作为属性。在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));
}
的代码是在反思沉重,但这是题外话。这段代码的日期时间返回的值是按以下格式: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(对象 值,字符串propertyName的,类型typeToBeConverted)
那么显然它不理解DateTime格式,但是当我看到的文档即是在这里贴:http://www.odata.org/developers/protocols/overview#AbstractTypeSystem
我希望它是有效的。有人对此有经验吗?
您的代码不会输出'Z' AFAICT,但您的错误消息肯定似乎暗示您的实际代码是。 –