将 XML 反序列化为对象属性时出现问题,最终为 null
Trouble Deserializing XML into Object properties end up null
我正在处理来自 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>
输出:
我通过逆向工程解决了这个问题,我将 类 序列化为 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>
它缺少 xml 架构命名空间。
我正在处理来自 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>
输出:
我通过逆向工程解决了这个问题,我将 类 序列化为 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>
它缺少 xml 架构命名空间。