我正在將資料結構序列化為 xml,但出現錯誤
資料結構
Public Class Product
Public Id As Integer
Public name As String
Public quantity As Integer
Public price As Integer
Public Property state As List(Of States)
Public Sub New()
End Sub
Public Sub New(ByVal name As String,
ByVal quantity As Integer,
ByVal price As Integer,
ByVal state As List(Of States)
)
Me.name = name
Me.quantity = quantity
Me.price = price
Me.state = state
End Sub
End Class
Public Class States
<XmlArrayItem("State")>
Public Property state As List(Of String)
Public Sub New()
End Sub
Public Sub New(state As List(Of String))
state = New List(Of String)()
End Sub
End Class
這就是我傳遞資料的方式
Dim countries As New List(Of Product) From
{
New Product With {.name = "Pakistan", .quantity = 1, .price = 50, .state = New List(Of States) From
{
New States With {.state = "KPK"},
New States With {.state = "Punjab"},
New States With {.state = "Sindh"}}},
New Product With {.name = "Saudi Arabia", .quantity = 2, .price = 100, .state = New List(Of States) From
{
New States With {.state = "Makkah"},
New States With {.state = "Madina"},
New States With {.state = "Jaddah"}}
}
}
我在狀態串列中收到錯誤,即(帶有 {.state = "Makkah"} 的新狀態)它說字串型別的值無法轉換為串列(字串)我以不同的方式嘗試過,但這個錯誤不會出現在任何地方
我的序列化程式代碼
Dim serialization As XmlSerializer = New XmlSerializer(GetType(List(Of Product)))
serialization.Serialize(Console.Out, countries)
Console.ReadLine()
我不確定這段代碼到底有什么問題。
uj5u.com熱心網友回復:
就像您創建一個List(Of States)for一樣Product,您需要創建一個List(Of String)forStates
Dim countries As New List(Of Product) From
{
New Product With {.name = "Pakistan", .quantity = 1, .price = 50, .state = New List(Of States) From
{
New States With {.state = New List(Of String) From {"KPK"}}, 'A list with one item
New States With {.state = New List(Of String) From {"Punjab", "Sindh"} 'A list with two items
}
}
轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/426201.html
