0

I have a VB class that contains an array of other VB classes. Then I serialize an object into xml using

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

my Classes:

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 

Untill now everything works fine (and my xml files is created). But when I replace

Public Property ISC() As myInnerSerializableClass 

With

Public Property ISC() As ICollection(Of myInnerSerializableCass)

I get an arror that reads : An error occurred during reflection of type 'myCustomNameSpace.mySerializableClass'

P.S. I need the ICollection for EF code first, or find a way to work with arrays in EF code first.

Ismail
  • 190
  • 11

1 Answers1

0

Apply the XmlArray and XmlArrayItem attributes to the property so that the serializer knows that the property should be serialized as a list.

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

Read more

Visual Vincent
  • 18,045
  • 5
  • 28
  • 75
  • The Error persists, I think the problem is that ICollection is an interface. – Ismail Mar 07 '17 at 16:59
  • @IsmailKarchi : [**Yup, you're right about that**](http://stackoverflow.com/a/15089253/3740093). Is there any other collection/list type you can use? -- Also, must you use XML serialization? If not, Binary Serialization is an alternative. – Visual Vincent Mar 07 '17 at 17:03
  • The objectif behind this is to create xml file from sql data. Do you know any workaround to replace ICollection in the class (needed for EntityFrameWork code first) – Ismail Mar 07 '17 at 17:11
  • @IsmailKarchi : I've never used EF, but from my understanding a [**`List(Of T)`**](https://msdn.microsoft.com/en-us/library/6sh2ey19(v=vs.110).aspx) should work. It implements the `ICollection(Of T)` interface. – Visual Vincent Mar 07 '17 at 18:48
  • 1
    Thank you @VisualVincent I replaced the `ICollection(Of T)` interface with a `List(Of T)` it worked well for EntityFrameWork but not for xml serialization because the related entities don't load. so I used the [**Eagerly Loading**](https://msdn.microsoft.com/en-us/library/jj574232(v=vs.113).aspx) – Ismail Mar 08 '17 at 07:27
  • @IsmailKarchi : Glad you found a solution! Good luck! – Visual Vincent Mar 08 '17 at 07:40