我是序列化的新手,遇到問題。我有一個返回IDDescriptionPair對象數組的REST服務。在使用服務時,我使用「將XML粘貼爲類型」VS加載項來創建對象。我只修改這個對象來添加DataContract屬性,所以我的名字空間在每一端都匹配。這裏是那個對象:使用WCF REST工具包使用ReadAsDataContract的問題
Imports System.Runtime.Serialization
<DataContract([Name]:="IDDescriptionPair", [Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary")>
<System.CodeDom.Compiler.GeneratedCodeAttribute("System.Xml", "4.0.30319.225"), _
System.Diagnostics.DebuggerStepThroughAttribute(), _
System.Xml.Serialization.XmlTypeAttribute([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary"), _
System.Xml.Serialization.XmlRootAttribute ([Namespace]:="http://schemas.datacontract.org/2004/07/Blizzard.ClassLibrary", IsNullable:=True)> _
Partial Public Class IDDescriptionPair
Private descriptionField As String
Private idField As Integer
Private idFieldSpecified As Boolean
'''<remarks/>
<System.Xml.Serialization.XmlElementAttribute(IsNullable:=True)> _
Public Property Description() As String
Get
Return Me.descriptionField
End Get
Set(value As String)
Me.descriptionField = value
End Set
End Property
'''<remarks/>
Public Property ID() As Integer
Get
Return Me.idField
End Get
Set(value As Integer)
Me.idField = value
End Set
End Property
'''<remarks/>
<System.Xml.Serialization.XmlIgnoreAttribute()> _
Public Property IDSpecified() As Boolean
Get
Return Me.idFieldSpecified
End Get
Set(value As Boolean)
Me.idFieldSpecified = value
End Set
End Property
End Class
我可以調用服務和反序列化的對象,它似乎工作正常。我得到了正確數量的IDDescriptionPair對象的列表。問題是它們都是空白的 - 沒有任何屬性被填充。
下面的代碼,我使用該服務:
Dim client As New HttpClient()
Dim endpoint As New Uri("http://bmpscnt410a/services/v1/personservices/offices/5/principals")
Using response As HttpResponseMessage = client.Get(endpoint)
response.EnsureStatusIsSuccessful()
Dim idp As List(Of IDDescriptionPair)
Try
idp = response.Content.ReadAsDataContract(Of List(Of IDDescriptionPair))()
Catch ex As Exception
End Try
End Using
我直接使用DataContractSerializer的嘗試,但我得到了相同的結果(預計,我猜)。任何想法,將不勝感激。
我應該添加response.Content.ReadAsString爲我提供了我期望的XML。 – Roger