WCF REST Xml响应键入

时间:2010-12-06 15:45:25

标签: xml wcf rest xml-serialization

我有一个WCF REST服务,它通过3种不同的方法公开了几十个对象并基于url格式路由调用。例如:

Protected Function [get](ByVal objType As String, ByVal id As String, ByVal propertyList As String, ByVal token As String) As Object

这些方法中的每一个都返回一个Object或BaseObj(我们返回的所有类型都从该对象继承)。但最终的问题是,当我查看XML时,我会得到一个包装器,如:

<ArrayOfBaseObj xmlns="http://objects/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <BaseObj i:type="Event">
        <!-- Event object details -->
    </BaseObj>
</ArrayOfBaseObj>

而不是键入的元素与返回的实际对象相同:

<ArrayOfEvent xmlns="http://objects/" xmlns:i="http://www.w3.org/2001/XMLSchema-instance">
    <Event>
        <!-- Event object details -->
    </Event>
</ArrayOfEvent>

有没有让XML序列化程序使用包装元素的类型名称而不是基本类型? JSON似乎正确地返回了类型,它只是我没有完全引用的xml序列化。

1 个答案:

答案 0 :(得分:0)

您需要添加属性[CollectionDataContract]。查看文章http://msdn.microsoft.com/en-us/library/dd203052.aspx了解详情。