字符串类型的值无法转换为列表(字符串)VB.Net
value of type string cannot be converted to list(of string) VB.Net
我正在将数据结构序列化为 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"}}
}
}
我在状态列表中收到错误,即 (New States With {.state = "Makkah"}) 它说类型字符串的值无法转换为列表(的字符串)我以不同的方式尝试过,但这个错误并没有消失
我的序列化器代码
Dim serialization As XmlSerializer = New XmlSerializer(GetType(List(Of Product)))
serialization.Serialize(Console.Out, countries)
Console.ReadLine()
我不确定这段代码到底有什么问题。
就像为 Product
创建 List(Of States)
一样,您需要为 States
创建 List(Of String)
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
}
}
我正在将数据结构序列化为 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"}}
}
}
我在状态列表中收到错误,即 (New States With {.state = "Makkah"}) 它说类型字符串的值无法转换为列表(的字符串)我以不同的方式尝试过,但这个错误并没有消失
我的序列化器代码
Dim serialization As XmlSerializer = New XmlSerializer(GetType(List(Of Product)))
serialization.Serialize(Console.Out, countries)
Console.ReadLine()
我不确定这段代码到底有什么问题。
就像为 Product
创建 List(Of States)
一样,您需要为 States
List(Of String)
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
}
}