开发者

How to deserialize object from OData Atom feed?

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"开发者_StackOverflow社区 />
  <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>

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.

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


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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜