Stan Bruce Stan Bruce - 1 year ago 93 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="">

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


<MessageContract( _
WrapperName:="OrderRequest", _
WrapperNamespace:="", _
IsWrapped:=true)> _
Public Class OrderRequest

<MessageBodyMember( _
[Namespace]:="", _
Order:=0)> _
Public TheOrder As Service.TheOrder

Public Sub New()
End Sub

Public Sub New(theOrder As Service.Order)
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=''> 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 Source

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

Dim xmlSerializer = New XmlSerializer( _
    GetType(Service.OrderRequest), _
    New XmlRootAttribute("OrderRequest") _
        With {.Namespace = ""})
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download