如何序列化一个类(有ICollection)

时间:2017-03-07 16:33:57

标签: vb.net entity-framework xsd entity-framework-6 xmlserializer

我有一个包含其他VB类数组的VB类。然后我使用

将对象序列化为xml
Private Sub Serialize(myObject As Object, myObjectType As Type)

        Dim ser As New XmlSerializer(myObjectType)
        Dim myWriter As StreamWriter = New StreamWriter("D:\DataTest\myXMLFile.xml")
        ser.Serialize(myWriter, myObject)
        myWriter.Close()

End Sub

我的课程:

Public Class mySerializableClass

        Public Property Name As String
        Public Property Adresse As String
        Public Property IdF As Integer
        Public Property ISC() As myInnerSerializableClass

End Class  

Public Class myInnerSerializableClass

        Public Property CNSS As String
        Public Property nom As String

End Class 

直到现在一切正常(我的xml文件已创建)。但是当我更换

Public Property ISC() As myInnerSerializableClass 

使用

Public Property ISC() As ICollection(Of myInnerSerializableCass)

我得到一个错误,上面写着:反映“myCustomNameSpace.mySerializableClass”类型时发生错误

P.S。我首先需要ICollection for EF代码,或者首先找到一种在EF代码中使用数组的方法。

1 个答案:

答案 0 :(得分:0)

XmlArrayXmlArrayItem属性应用于属性,以便序列化程序知道该属性应序列化为列表。

<XmlArray("ISC"), XmlArrayItem("myInnerSerializableClass")> _
Public Property ISC() As ICollection(Of myInnerSerializableCass)

了解更多