Reputation: 725
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. However, the attributes from ReqBody2Internal are all null.
The OperationContract is:
[OperationContract]
[WebInvoke(UriTemplate = "Invoke2",RequestFormat=WebMessageFormat.Xml , ResponseFormat=WebMessageFormat.Xml)]
void Invoke2(ReqBody2 req);
This is an example Xml I'm posting using Fiddler:
<?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.
Any clue as to why this might be happening?
Upvotes: 1
Views: 1519
Reputation: 39685
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. This includes the Req
element which will be deserialized into an element of type ReqBody2Internal
.
However your ReqBody2Internal
type has a namespace declared as 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.
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.
Upvotes: 2