Asp.net 可序列化类集合
我有一个类,它包含一个属性列表,可以很好地序列化。但是,我需要这些属性中的一个来包含另一个类的集合,以便为我提供子类 示例XMLAsp.net 可序列化类集合,asp.net,vb.net,Asp.net,Vb.net,我有一个类,它包含一个属性列表,可以很好地序列化。但是,我需要这些属性中的一个来包含另一个类的集合,以便为我提供子类 示例XML <VideoOptions> <property1>value1</property1> <property2>value2</property2> <property3> <item> <property1>value1</property1
<VideoOptions>
<property1>value1</property1>
<property2>value2</property2>
<property3>
<item>
<property1>value1</property1>
</item>
<item>
<property1>value1</property1>
</item>
</property3>
</VideoOptions>
您只需在VideoOptions类上拥有一个属性,该类是Property3的集合 创建一个集合类,如下所示
Public Class Property3Collection
Inherits CollectionBase
Public Function Add(ByVal item As Property3Item) As Integer
Return Me.List.Add(item)
End Function
Default Public ReadOnly Property Item(ByVal index As Integer) As Object
Get
Return CType(Me.List.Item(index), SingleItem)
End Get
End Property
End Class
为您的项目创建一个类
Public Class Property3Item
'Add in Item property
End Class
建立您的Property3Collection类
Public Sub AddPropertyItem
Dim newCol as New Property3Collection
newCol.Add(New Property3Item)
End Sub
将属性添加到VideoOptions类
Public Property Property3() As Property3Collection
Get
End Get
Set(ByVal Value As Property3)
End Set
End Property
只要Property3Item的构造函数没有参数(xml序列化需要),Xmlserializer类就会将其序列化并反序列化为您指定的格式,而不会出现问题
希望这有帮助
Public Property Property3() As Property3Collection
Get
End Get
Set(ByVal Value As Property3)
End Set
End Property