将XML反序列化为对象属性的麻烦最终为空

时间:2018-11-08 16:55:32

标签: xml vb.net visual-studio deserialization xml-serialization

我正在处理SOAP Web服务调用的响应。当客户端对象中的方法尝试反序列化响应时,它什么都没有。

我已经建立了一个模拟反序列化过程的测试,但是仍然无法获取IsSuccess和Message的值来填充对象。

请在这里帮助我确定缺少的内容,因此反序列化过程将把xml完全反序列化为相应的对象。合同代码是使用svcutil自动生成的。

编辑:我可以根据需要使用messageinspector处理xml,因此,如果问题出在xml上,我只需要弄清楚要更改的内容。

合同条款:

<System.CodeDom.Compiler.GeneratedCodeAttribute("svcutil", "4.0.30319.33440"),
 System.SerializableAttribute(),
 System.Diagnostics.DebuggerStepThroughAttribute(),
 System.ComponentModel.DesignerCategoryAttribute("code"),
 System.Xml.Serialization.XmlTypeAttribute(AnonymousType:=True, [Namespace]:="Green:EggsandHam.wsdll")>
Partial Public Class NumberResponse

    Private isSuccessField As String

    Private messageField As String

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Order:=0)>
    Public Property IsSuccess() As String
        Get
            Return Me.isSuccessField
        End Get
        Set(value As String)
            Me.isSuccessField = value
        End Set
    End Property

    '''<remarks/>
    <System.Xml.Serialization.XmlElementAttribute(Order:=1)>
    Public Property Message() As String
        Get
            Return Me.messageField
        End Get
        Set(value As String)
            Me.messageField = value
        End Set
    End Property
End Class


<System.Diagnostics.DebuggerStepThroughAttribute(),
 System.CodeDom.Compiler.GeneratedCodeAttribute("System.ServiceModel", "4.0.0.0"),
 System.ComponentModel.EditorBrowsableAttribute(System.ComponentModel.EditorBrowsableState.Advanced),
 System.ServiceModel.MessageContractAttribute(IsWrapped:=False)>
Partial Public Class NumberResponse1

    <System.ServiceModel.MessageBodyMemberAttribute([Namespace]:="Green:EggsandHam.wsdl", Order:=0)>
    Public NumberResponse As NumberResponse

    Public Sub New()
        MyBase.New()
    End Sub

    Public Sub New(ByVal NumberResponse As NumberResponse)
        MyBase.New()
        Me.NumberResponse = NumberResponse
    End Sub
End Class

测试:

Imports System.IO
Imports System.Text
Imports System.Xml.Serialization

Module Module1

    Sub Main()
        Dim u As New NumberResponse1()
        u = Deserialize(Of NumberResponse1)(XElement.Load("C:\test\number.xml"))
        Console.WriteLine(u.NumberResponse.Message)
    End Sub

    Public Function Deserialize(Of T)(ByVal xElement As XElement) As T
        Using memoryStream As New MemoryStream(Encoding.ASCII.GetBytes(xElement.ToString()))
            Dim xmlSerializer As New XmlSerializer(GetType(T))
            Return xmlSerializer.Deserialize(memoryStream)
        End Using
    End Function

End Module

XML:

<?xml version="1.0" encoding="utf-8"?>
<NumberResponse1>
    <NumberResponse>
        <ns1:NumberResponse xmlns:ns1="Green:EggsandHam.wsdl">
            <ns1:IsSuccess>true</ns1:IsSuccess>
            <ns1:Message>number processed successfully.</ns1:Message>
        </ns1:NumberResponse>
    </NumberResponse>
</NumberResponse1>

输出:

enter image description here

1 个答案:

答案 0 :(得分:0)

我通过反向工程解决了这个问题,我将类序列化为xml。新的xml最终看起来像这样:

<?xml version="1.0" encoding="utf-8"?>
<NumberResponse1 xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xmlns:xsd="http://www.w3.org/2001/XMLSchema">
    <NumberResponse>
        <IsSuccess xmlns="Green:EggsandHam.wsdl">True</IsSuccess>
        <Message xmlns="Green:EggsandHam.wsdl">your father smelt of elderberries</Message>
    </NumberResponse>
</NumberResponse1>

它缺少xmlschema命名空间。