如何从 OData Atom feed 反序列化对象?
我正在尝试解析来自 OData REST 服务的响应。当响应采用 JSON 格式时,可以轻松使用 WCF REST 入门工具包中的 ReadAsJsonDataContract
方法。然而,如果响应是 Atom feed,事情似乎会更加复杂。这是一个示例:
<?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 来处理。
有谁知道可以使用什么技术来反序列化 OData 原子 m:properties 元素的内容?
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>
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
WCF 数据服务有一个客户端,可用于使用响应并从中具体化 CLR 对象。查看 System.Data.Services.Client.DataServiceContext 类和所有相关类。
事实上,在 VS 中,您可以向 OData 服务“添加服务引用”,它将生成服务的客户端类以及来自
DataServiceContext
的派生类供您使用。如果您已经拥有客户端类,则可以使用
DataServiceContext.Execute
方法发出任何查询并将其结果具体化为客户端类型。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.