简体   繁体   English

WCF RestFul服务上的XML反序列化

[英]XML deserializing on WCF RestFul service

I've got the following classes (Don't mind the Namespaces): 我有以下课程(不要介意命名空间):

[DataContract(Namespace = "http://www.test.com/ReqBody2")]
[KnownType(typeof(ReqBody2))]
public class ReqBody2
{
    [DataMember]
    public string pass { get; set; }
    [DataMember]
    public int Tout { get; set; }
    [DataMember]
    public string RequestDate { get; set; }
    [DataMember]
    public ReqBody2Internal Req { get; set; }
    [DataMember]
    public string ReqEnc { get; set; }
}

[DataContract(Namespace = "http://www.test.com/ReqBodyInternal")]
[KnownType(typeof(ReqBody2Internal))]
public class ReqBody2Internal
{
    [DataMember]
    public string Field1 { get; set; }
    [DataMember]
    public string Field2 { get; set; }
    [DataMember]
    public string Field3 { get; set; }
    [DataMember]
    public string Field4 { get; set; }
}

When I post the Xml Serialization of ReqBody2, the service receives and deserializes the object's root attributes properly. 当我发布ReqBody2的Xml序列化时,该服务会正确接收并反序列化对象的根属性。 However, the attributes from ReqBody2Internal are all null. 但是,来自ReqBody2Internal的属性都为空。

The OperationContract is: OperationContract是:

[OperationContract]
[WebInvoke(UriTemplate = "Invoke2",RequestFormat=WebMessageFormat.Xml , ResponseFormat=WebMessageFormat.Xml)]
void Invoke2(ReqBody2 req);

This is an example Xml I'm posting using Fiddler: 这是我使用Fiddler发布的Xml示例:

<?xml version="1.0" encoding="utf-8"?><ReqBody2 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns="http://www.test.com/ReqBody2">
<pass>HOLA</pass>
<Req><Field1>asd</Field1><Field2>asd</Field2><Field3>asd</Field3><Field4>extra value</Field4></Req>
<RequestDate>2013-04-04T14:10:38</RequestDate>
<Tout>30000</Tout>
</ReqBody2>

What I expect to happen is to have access to Req attributes, but they are null on the server. 我希望发生的事情是可以访问Req属性,但是在服务器上它们为null。

Any clue as to why this might be happening? 关于为什么会发生这种情况的任何线索?

Your document being posted has a default namespace defined with: 您发布的文档具有默认名称空间,该名称空间定义为:

xmlns="http://www.test.com/ReqBody2"

This means that unless specified, all child elements will inherit this XML namespace. 这意味着,除非指定,否则所有子元素都将继承此XML名称空间。 This includes the Req element which will be deserialized into an element of type ReqBody2Internal . 这包括Req元素,该元素将反序列化为ReqBody2Internal类型的元素。

However your ReqBody2Internal type has a namespace declared as http://www.test.com/ReqBodyInternal . 但是,您的ReqBody2Internal类型具有一个声明为http://www.test.com/ReqBodyInternal的命名空间。 This means the child XML elements are expected to be from this namespace to deseralize correctly, but they inherit the default namespace and thus are seen as the "wrong" elements by the serializer. 这意味着期望子XML元素来自此命名空间以正确反序列化,但它们继承了默认命名空间,因此被序列化程序视为“错误”元素。

To fix this, you need to change the namespace declaration on your data contracts to share the same namespace, or change your XML to specify the correct namespace for the child elements of the Req element. 要解决此问题,您需要更改数据协定上的名称空间声明以共享同一名称空间,或者更改XML以为Req元素的子元素指定正确的名称空间。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM