我收到以下异常:当我尝试序列化我的课时,出现了反映“ValoradorHC.estruturas.dispoHotel”类型的错误。
我的代码是:
Public Function getXMLdeObjecto(ByVal obj As Object)
Dim ser As New XmlSerializer(obj.GetType())
Dim sb As New System.Text.StringBuilder()
Dim writer As New System.IO.StringWriter(sb)
ser.Serialize(writer, obj)
Return sb.ToString()
End Function
我正在尝试序列化的课程:
Public Class estruturas
<Serializable()> _
Public Class PedidoDispo
Private myLocalidade As String
Public mydataInicio As Date
Public mydataFim As Date
Public mynumQuartos As Integer
Public mynumPessoas As Integer
Public myordenar As String
Public myidioma As String
Public Property Localidade() As String
Get
Return myLocalidade
End Get
Set(ByVal Value As String)
myLocalidade = Value.Replace(" ", "_")
End Set
End Property
Public Property dataInicio() As String
Get
Return mydataInicio
End Get
Set(ByVal Value As String)
mydataInicio = Value.Replace(" ", "_")
End Set
End Property
Public Property dataFim() As String
Get
Return mydataFim
End Get
Set(ByVal Value As String)
mydataFim = Value.Replace(" ", "_")
End Set
End Property
Public Property numQuartos() As String
Get
Return mynumQuartos
End Get
Set(ByVal Value As String)
mynumQuartos = Value.Replace(" ", "_")
End Set
End Property
Public Property numPessoas() As String
Get
Return mynumPessoas
End Get
Set(ByVal Value As String)
mynumPessoas = Value.Replace(" ", "_")
End Set
End Property
Public Property ordenar() As String
Get
Return myordenar
End Get
Set(ByVal Value As String)
myordenar = Value.Replace(" ", "_")
End Set
End Property
Public Property idioma() As String
Get
Return myidioma
End Get
Set(ByVal Value As String)
myidioma = Value.Replace(" ", "_")
End Set
End Property
Sub New(ByVal localidade As String, ByVal dataInicio As String, ByVal dataFim As String, ByVal numQuartos As Integer, ByVal numPessoas As Integer)
Me.Localidade = localidade
Me.dataInicio = DateTime.Parse(dataInicio)
Me.dataFim = DateTime.Parse(dataFim)
Me.numQuartos = numQuartos
Me.numPessoas = numPessoas
ordenar = "Popularity-desc"
idioma = "PT"
End Sub
End Class
<Serializable()> _
Public Class dispoHotel
Private myPedido As PedidoDispo
Private mylocalidade As String
Private mylistaHotel As List(Of hotel)
Public ReadOnly Property qtdHotels() As String
Get
Return mylistaHotel.Count
End Get
End Property
Public Property listaHotel() As List(Of hotel)
Get
Return mylistaHotel
End Get
Set(ByVal Value As List(Of hotel))
mylistaHotel = Value
End Set
End Property
Public Property pedido() As PedidoDispo
Get
Return myPedido
End Get
Set(ByVal Value As PedidoDispo)
myPedido = Value
End Set
End Property
Sub New(ByVal pedido As PedidoDispo)
Me.pedido = pedido
listaHotel = New List(Of hotel)
End Sub
Sub New()
listaHotel = New List(Of hotel)
End Sub
End Class
Public Structure hotel
Dim id As String
Dim nome As String
Dim categoria As Integer
Dim morada As String
Dim caracteristicas As List(Of String)
Dim distancia As String
Dim foto As String
Dim melhorPreco As String
Dim precos As Hashtable
End Structure
End Class
答案 0 :(得分:2)
您的dispoHotel类有一个只读属性qtdHotels。尝试使用XMLIgnore属性
使序列化程序跳过它答案 1 :(得分:1)
您是否尝试过对象酒店可序列化?