locked
Serialization class (vb.net) wpf application RRS feed

  • Question

  • Hi everybody,
    Some time ago I'm blocked with the serialization of a Class.  I called "Acquisition". Every time that i try to serialize this class, I always get an error like this:
     
    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
     
    Additional information: An error occurred during the reflection type 'Audit_Logiciel.Acquisition'.
     
    So, i was wondering where is the Error? I can't find it.
    Here is my program:

    First, the Class:

    Imports System.IO
    Imports System.Xml.Serialization
    Imports System.Runtime.Serialization
    Imports System.ComponentModel
     
    
    <Serializable()>
    <XmlRoot("Acquisition")> Public Class Acquisition
        Public Sub New()
            InitializeComponent()
        End Sub
    #Region "Declarations"
     
        
        Public _TextBoxNomModule As String
        Public _TextBoxModem As String
       
     
    #End Region
     
    
     
    #Region "Propiétés"
     
     
     
        <XmlAttribute("Modem")> Public Property NomModule() As String
            Get
                Return _TextBoxNomModule
            End Get
            Set(ByVal value As String)
                _TextBoxNomModule = value
                'OnPropertyChanged("Modem")
            End Set
        End Property
     
        <XmlAttribute("Modem")> Public Property Modem() As String
            Get
                Return _TextBoxModem
            End Get
            Set(ByVal value As String)
                _TextBoxModem = value
                'OnPropertyChanged("Modem")
            End Set
        End Property
     
    
        
    #Region "Métodes"
     
        Sub New(ByVal nommodule As String, ByVal modem As String)
            _TextBoxNomModule = nommodule
            _TextBoxModem = modem
             InitializeComponent()
        End Sub
    #End Region
     
    End Class
    

    Then the Class where i call the serialization:

    Imports System.IO
    Imports System.Text
    Imports System.Windows.Forms
    Imports System.Xml
    Imports System.Xml.Serialization
     
    Public Class JriTechnologie
       
     
    Private Sub ButtonAdd_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles ButtonAdd.Click
        Dim JRIs As New Acquisition(_TextBoxNomModule.Text, _TextBoxModem.Text)
     
        Dim FluxDeFichier As FileStream = File.Create("Test.xml")
        Dim Serialiseur As New XmlSerializer(GetType(Acquisition))
         Serialiseur.Serialize(FluxDeFichier, JRIs)
      
        FluxDeFichier.Close()

    End Class




    Finally, I must say that the Acquisition class corresponds to a new window that opens when I press a button (click), here is the part of the program

    Public WindowAcqui As Acquisition
     
    Public Sub button1_Click(ByVal sender As System.Object, ByVal e As System.Windows.RoutedEventArgs) Handles button1.Click
     
      
         WindowAcqui = New Acquisition
         WindowAcqui.ShowDialog()
     
    
     End Sub

    So, somebody can help me with problem.?
     

    An unhandled exception of type 'System.InvalidOperationException' occurred in PresentationFramework.dll
     
    Additional information: An error occurred during the reflection type 'Audit_Logiciel.Acquisition'.
    Thanks    
    Monday, July 8, 2013 9:13 AM

Answers

  • It seems that you are trying to serialize the Acquisition class which is a WPF Window. This is an unusual operation since the class contains several other members which you do not need to appear in XML.

    Try defining a class that contains only data for serialization. For example:

    <Serializable()>
    <XmlRoot("Acquisition")>
    Public Class AcquisitionData
    
        <XmlAttribute("NomModem")> Public Property NomModule() As String
    
        <XmlAttribute("Modem")> Public Property Modem() As String
    
    End Class

    Then execute this:

    Dim JRIs As New AcquisitionData With {.NomModule = _TextBoxNomModel.Text, .Modem = _TextBoxModem.Text}
    
    Using FluxDeFichier As FileStream = File.Create("Test.xml")
        Dim Serialiseur As New XmlSerializer(GetType(AcquisitionData))
        Serialiseur.Serialize(FluxDeFichier, JRIs)
    End Using


    • Edited by Viorel_MVP Monday, July 8, 2013 5:18 PM 255+1
    • Marked as answer by icor Wednesday, July 10, 2013 9:51 AM
    Monday, July 8, 2013 5:17 PM

All replies

  • It seems that you are trying to serialize the Acquisition class which is a WPF Window. This is an unusual operation since the class contains several other members which you do not need to appear in XML.

    Try defining a class that contains only data for serialization. For example:

    <Serializable()>
    <XmlRoot("Acquisition")>
    Public Class AcquisitionData
    
        <XmlAttribute("NomModem")> Public Property NomModule() As String
    
        <XmlAttribute("Modem")> Public Property Modem() As String
    
    End Class

    Then execute this:

    Dim JRIs As New AcquisitionData With {.NomModule = _TextBoxNomModel.Text, .Modem = _TextBoxModem.Text}
    
    Using FluxDeFichier As FileStream = File.Create("Test.xml")
        Dim Serialiseur As New XmlSerializer(GetType(AcquisitionData))
        Serialiseur.Serialize(FluxDeFichier, JRIs)
    End Using


    • Edited by Viorel_MVP Monday, July 8, 2013 5:18 PM 255+1
    • Marked as answer by icor Wednesday, July 10, 2013 9:51 AM
    Monday, July 8, 2013 5:17 PM
  • Thank you,

    The problem is fixed. Tha was the god answer.

    Thank you very much.

    Wednesday, July 10, 2013 9:52 AM