none
Serialiazable class . Creating an XML file using a list (of Object) RRS feed

  • Question

  • I am trying to create an XML file having a List ( of ReconSources) where the ReconSources class is the base class  for each different file type class i have created.

    The issue here is that when i use XMLserializer class in order to do that I am getting an error that my file type class is not using a parameterless constructor.

    The base class gets as parameters the file name and the path of the file as well as the type of the file.

    Each class that inherit from this Recon Sources base class also has a constructor with parameters the above filename,filepath and filetype.

    So in general the concept of creating a parameterless constructor cannot be used here.

    My question is if there is a way to create from a list an xml file using a different way?

    Or if there is a way to pass this issue with the parameterless constructor when using the XMLSerializer?

    Please advise and thank you in advance.

    Tuesday, November 5, 2013 10:40 AM

Answers

  • I spent hours on this one and the answer is very stupid.. Do what the error messages says.  I put two constructors into ReconSource and the error went away.  See code below.

    Imports System.IO
    Imports System
    Imports System.Xml
    Imports System.Xml.Serialization
    Imports System.Xml.Schema
    Module Module1
        Public Const MYFILENAME = "c:\temp\test.xml"
        Sub Main()
            Dim rsdf As New List(Of ReconSource)
            Call createxmlRSDF(rsdf)
        End Sub
        Public Sub createxmlRSDF(ByVal rsdf As List(Of ReconSource))
            Dim filename As String = MYFILENAME
            Dim swriter As New StreamWriter(filename)
            Dim writer As XmlWriter = XmlWriter.Create(swriter)
            Dim serializer As New XmlSerializer(rsdf.GetType())
            Using swriter
                Using writer
                    serializer.Serialize(writer, rsdf)
                    writer.Close()
                End Using
            End Using
        End Sub
    End Module
    Public Class ReconSource
        Sub New(ByVal data As String)
        End Sub
        Sub New()
        End Sub
    End Class


    jdweng

    Tuesday, November 5, 2013 5:55 PM

All replies

  • You are having a problem because you are trying to serailize an array object.  Arrays have to be serailzie a little different then a single object.  See webpage below

    http://stackoverflow.com/questions/2679490/how-to-serialize-listobject


    jdweng

    Tuesday, November 5, 2013 10:56 AM
  • The link is not really helpful to be honest.

    Do you have any other links that I can view and understand the differences?

    Please advise.

    Tuesday, November 5, 2013 12:29 PM
  • I am still getting the same issue even though following the Array serialize way.

    Unless there is something I missed here.

    Basically this is the simple sub that I am trying to create.

    Where the rsdf list(of reconsource) basically at this point has 11 different entries each one from a different file class object that all inherit though from the ReconSource base class.

    Public Sub createxmlRSDF(rsdf As List(Of ReconSource))

                Dim filename As String = "C:\Documents and Settings\StavrosAndreou\My Documents\Tools\LIBRA\C4\Workbooks\test.xml"

                Dim swriter As New StreamWriter(filename)

                Dim writer As XmlWriter = XmlWriter.Create(swriter)

                Dim serializer As New XmlSerializer(GetType(List(Of ReconSource)))

                Using swriter

                    Using writer

                        serializer.Serialize(writer, rsdf)

                        writer.Close()

                    End Using

                End Using

            End Sub

    Tuesday, November 5, 2013 12:36 PM
  • The webpage I posted had a response where a change was made equivalent to the change below

    From : Dim serializer As New XmlSerializer(GetType(List(Of ReconSource)))
    To : Dim serializer As New XmlSerializer(rsdf.GetType())


    jdweng

    Tuesday, November 5, 2013 1:17 PM
  • Still receiving the same issue after the change suggested that my file type class is not using a parameterless constructor.

    As I said at the begging I don't have a constructor without parameters either in the base class nor in the rest of the classes inheriting from the ReconSource class.

    That is the reason I am trying to find a way to do this.

    Please advise and btw thx for the help so far honestly this has been a pain for a really long time now.

    Tuesday, November 5, 2013 1:39 PM
  • What line is the error occuring on?

    jdweng

    Tuesday, November 5, 2013 1:54 PM
  • on

    serializer.Serialize(writer,rsdf)

    For testing purposes I am only right now processing and testing one specific file so I am only using only one file class that and so its producing the error on that specific file source.

    Tuesday, November 5, 2013 1:58 PM
  • I spent hours on this one and the answer is very stupid.. Do what the error messages says.  I put two constructors into ReconSource and the error went away.  See code below.

    Imports System.IO
    Imports System
    Imports System.Xml
    Imports System.Xml.Serialization
    Imports System.Xml.Schema
    Module Module1
        Public Const MYFILENAME = "c:\temp\test.xml"
        Sub Main()
            Dim rsdf As New List(Of ReconSource)
            Call createxmlRSDF(rsdf)
        End Sub
        Public Sub createxmlRSDF(ByVal rsdf As List(Of ReconSource))
            Dim filename As String = MYFILENAME
            Dim swriter As New StreamWriter(filename)
            Dim writer As XmlWriter = XmlWriter.Create(swriter)
            Dim serializer As New XmlSerializer(rsdf.GetType())
            Using swriter
                Using writer
                    serializer.Serialize(writer, rsdf)
                    writer.Close()
                End Using
            End Using
        End Sub
    End Module
    Public Class ReconSource
        Sub New(ByVal data As String)
        End Sub
        Sub New()
        End Sub
    End Class


    jdweng

    Tuesday, November 5, 2013 5:55 PM