Stan Bruce Stan Bruce - 1 month ago 14
Vb.net Question

Issue deserializing XML from SOAP request

I'm having difficulties deserializing XML from a SOAP request to an object.

The XML looks like this (I have control over this, so this can be altered):

<v001:OrderRequest xmlns:v001="http://service.test.com/ServiceMessages/v001">
<v001:TheOrder>
...
</v001:TheOrder>
</v001:OrderRequest>


This is (part of) the object (I have no control over this):

...

<MessageContract( _
WrapperName:="OrderRequest", _
WrapperNamespace:="http://service.test.com/ServiceMessages/v001", _
IsWrapped:=true)> _
Public Class OrderRequest

<MessageBodyMember( _
[Namespace]:="http://service.test.com/ServiceMessages/v001", _
Order:=0)> _
Public TheOrder As Service.TheOrder

Public Sub New()
MyBase.New
End Sub

Public Sub New(theOrder As Service.Order)
MyBase.New
Me.TheOrder = theOrder
End Sub
End Class

...


And this is the code I use to deserialize:

...

Dim xmlString = requestEnvelope.Body.InnerXml
Dim orderRequest As Service.OrderRequest = Nothing

Using xmlTextReader = New XmlTextReader(New MemoryStream(xmlString))
Dim xmlSerializer = New XmlSerializer(GetType(Service.OrderRequest), New XmlRootAttribute("OrderRequest"))

orderRequest = CType(xmlSerializer.Deserialize(xmlTextReader), Service.OrderRequest)
End Using

...


But I keep getting the following error:

There is an error in XML document (1, 2).
<OrderRequest xmlns='http://service.test.com/ServiceMessages/v001'> was not expected.


I've tried stripping namespaces, prefixes, omitting the XmlRootAttribute parameter for the XmlSerializer, ... resulting in different error messages or empty objects...
Is there something I'm missing or doing wrong? I've been staring at this for almost 2 days now and I fear I'm just overlooking something.

Answer

Providing the Namespace for the XmlRootAttribute of the XmlSerializer did the job:

Dim xmlSerializer = New XmlSerializer( _
    GetType(Service.OrderRequest), _
    New XmlRootAttribute("OrderRequest") _
        With {.Namespace = "http://service.test.com/ServiceMessages/v001"})