大家好,我正在尝试找出处理这些嵌套列表对象的有效方法,在这种情况下,从XML反序列化。例如这个模型结构
public class Car
public property wheels as List(Of Wheels)
public property BearingType as String
end class
public class Wheels
public property bearings as List(Of Bearings)
end class
public class Bearings
public property BearingType as String
end class
假设我像这样反序列化了2个Car对象
Car 1 (
Wheel 1 (Bearing 1 (BearingType Alloy) , Bearing 2(BearingType Alloy)
Wheel 2 (Bearing 3 (BearingType Alloy) , Bearing 4(BearingType Alloy)
)
Car 2(
Wheel 1 (Bearing 1 (BearingType Metal) , Bearing 2(BearingType Metal)
Wheel 2 (Bearing 3 (BearingType Metal) , Bearing 4(BearingType Metal)
)
由于我确定一辆汽车只有一种类型的BearingType,我如何将轴承类型分配给父级?所以我可以通过Car.BearingType而不是Car.Wheels(0).Bearings(0).BearingType来访问它。
答案 0 :(得分:0)
那么XML结构是由其他人定义的吗?然后我会保留它,如果你是shure它将只有一个然后你可以解决这个快速和脏:
Public Class Car
Public Property wheels As List(Of Wheels)
Public Property BearingType As String
Get
Return Me.wheels(0).bearings(0).BearingType()
End Get
Set(value As String)
For Each wheel In Me.wheels
For Each bearing In wheel.bearings
bearing.BearingType = value
Next
Next
End Set
End Property
End Class
或更好:实施IXmlSerializable。
否则重新格式化xml文件并更新您的类。