从SOAP请求发出反序列化XML的问题

时间:2014-09-10 09:01:02

标签: .net xml vb.net soap xml-serialization

我很难将XML从SOAP请求反序列化到对象。

XML看起来像这样(我可以控制它,所以可以改变它):

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

这是对象的一部分(我无法控制):

...

<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

...

这是我用来反序列化的代码:

...

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

...

但我一直收到以下错误:

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

我尝试过删除命名空间,前缀,省略XmlSerializer的XmlRootAttribute参数,......导致不同的错误消息或空对象...... 我有什么遗失或做错的事吗?我现在已经盯着这看了将近两天,我担心我只会忽略一些事情。

1 个答案:

答案 0 :(得分:2)

为XmlSerializer的XmlRootAttribute提供命名空间完成了这项工作:

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