大家好,我正在设法找出有效的方法来处理这些嵌套的列表对象,在本例中,从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来访问它。
发布于 2014-07-25 10:26:49
那么XML-结构是由其他人定义的吗?那么我就留着它,如果你是舒尔,它就只有一个,那么你就能解决这个既快又脏的问题:
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文件并更新类。
https://stackoverflow.com/questions/24950632
复制相似问题