XML Serialization of iCollection(of t)

S

Sid Price

I have a class that implements ICollection(Of t):
Public Class SignalCollection

Implements ICollection(Of VM_Signal)

Public Sub Add(ByVal oSignal As VM_Signal) Implements ICollection(Of
VM_Signal).Add

....

End Sub

....

End Class

It implements all of the necessary methods and properties and seems to work
just fine. Now I need to serialize this object to an XML file and I am
trying to use XMLSerializer. Here is my code for that:

Dim x As XmlSerializer = New XmlSerializer(GetType(SignalCollection))

Dim writer As TextWriter

writer = New StreamWriter("MyFile.xml")

x.Serialize(writer, Me)

writer.Close()

When XMLSerializer object is created I get an exception:

System.InvalidOperationException was unhandled
Message="An error occurred creating the form. See Exception.InnerException
for details. The error is: To be XML serializable, types which inherit from
IEnumerable must have an implementation of Add(System.Object) at all levels
of their inheritance hierarchy. VM_Signals.SignalCollection does not
implement Add(System.Object)."
Source="Send_xml"
StackTrace:
at Send_xml.My.MyProject.MyForms.Create__Instance__[T](T Instance) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 190
at Send_xml.My.MyProject.MyForms.get_frmMain()
at Send_xml.My.MyApplication.OnCreateMainForm() in C:\Data
Root\Projects\Pacific\Code\VistaMax\Tools 500 Series\VistaMax Network
Manager\Test Applications\Send_xml\My Project\Application.Designer.vb:line
35
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.OnRun()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.DoApplicationModel()
at
Microsoft.VisualBasic.ApplicationServices.WindowsFormsApplicationBase.Run(String[]
commandLine)
at Send_xml.My.MyApplication.Main(String[] Args) in
17d14f5c-a337-4978-8281-53493378c1071.vb:line 81
at System.AppDomain._nExecuteAssembly(Assembly assembly, String[]
args)
at System.AppDomain.ExecuteAssembly(String assemblyFile, Evidence
assemblySecurity, String[] args)
at Microsoft.VisualStudio.HostingProcess.HostProc.RunUsersAssembly()
at System.Threading.ThreadHelper.ThreadStart_Context(Object state)
at System.Threading.ExecutionContext.Run(ExecutionContext
executionContext, ContextCallback callback, Object state)
at System.Threading.ThreadHelper.ThreadStart()


I have an Add method for my object so I do not understand what more I need
to add. All the documentation I can find relates to serializing ICollection
derived classes and not ICollection(Of t) objects. Any pointers to a sample
or documentation would be much appreciated.

Sid.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top