本文介绍了如何从OData Atom提要中反序列化对象?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试分析来自OData REST服务的响应.当响应为JSON格式时,可以轻松使用WCF REST入门工具包中的ReadAsJsonDataContract方法.但是,如果响应是Atom提要,情况似乎会更加复杂.这是一个例子:

I am trying to parse response from an OData REST service. When response is in JSON format, it is easy to use ReadAsJsonDataContract method from WCF REST starter kit. However things seem to be more complicated in case the response is an Atom feed. This is an example:

<?xml version="1.0" encoding="iso-8859-1" standalone="yes"?>
<entry xml:base="http://localhost:64172/BookshopService.svc/" xmlns:d="http://schemas.microsoft.com/ado/2007/08/dataservices" xmlns:m="http://schemas.microsoft.com/ado/2007/08/dataservices/metadata" xmlns="http://www.w3.org/2005/Atom">
  <id>http://localhost:64172/BookshopService.svc/Books(89)</id>
  <title type="text"></title>
  <updated>2010-11-08T09:44:21Z</updated>
  <author>
    <name />
  </author>
  <link rel="edit" title="Books" href="Books(89)" />
  <link rel="http://schemas.microsoft.com/ado/2007/08/dataservices/related/OrderLines" type="application/atom+xml;type=feed" title="OrderLines" href="Books(89)/OrderLines" />
  <category term="BookshopModel.Books" scheme="http://schemas.microsoft.com/ado/2007/08/dataservices/scheme" />
  <content type="application/xml">
    <m:properties>
      <d:Id m:type="Edm.Int32">89</d:Id>
      <d:Author>Martin Fowler</d:Author>
      <d:Title>Analysis Patterns</d:Title>
      <d:Price m:type="Edm.Decimal">50.20</d:Price>
    </m:properties>
  </content>
</entry>

因此,实际对象在"content/m:properties"元素中序列化.当然,DataContractSerializer不能期望这种模式.

So the actual object is serialized in "content/m:properties" element. And of course this can't be handled by DataContractSerializer that expects a different schema.

有人知道可以使用什么技术对OData atom m:properties元素的内容进行反序列化吗?

Does anyone know what technique can be used to deserialize the content of OData atom m:properties element?

推荐答案

WCF数据服务有一个客户端,可以用来使用响应并从中实现CLR对象.看一下System.Data.Services.Client.DataServiceContext类和所有相关的类.实际上,在VS中,您可以向OData服务添加服务引用",它将为服务生成客户端类以及DataServiceContext的派生类供您使用.如果已经有了客户端类,则可以使用DataServiceContext.Execute<T>方法发出任何查询并将其结果具体化为客户端类型.

WCF Data Services has a client which can be used to consume the responses and materialize CLR object from those. Take a look at the System.Data.Services.Client.DataServiceContext class and all related classes.In fact, in VS you can "Add Service Reference" to your OData services and it will generate client-side classes for the services as well as a derived class from the DataServiceContext for you to use.If you already have the client side classes you can use the DataServiceContext.Execute<T> method to issue any query and materialize its results into the client side types.

这篇关于如何从OData Atom提要中反序列化对象?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

09-02 01:13