locked
Deserializing objects RRS feed

  • Question

  • Hi,

    I have a class called "Project" i serialize project to xml with below code and works fine.  

      Public ProjectsList As New BindingList(Of Project)
        Public Sub SerializeObj(obj As Object, ByVal path As String)
    
            Dim xs As New System.Xml.Serialization.XmlSerializer(obj.GetType)
            Dim w As New StreamWriter(path)
            xs.Serialize(w, obj)     
    
        End Sub

    and i initialize my application like this with bindingsource

            BS_ProjectsList.DataSource = ProjectsList
            DataGridView4.DataSource = BS_ProjectsList

    After initialization i am trying to load serialized xml to ProjectsList like below 

    ProjectsList = deSerializeObj(ProjectsList, fPath)
        Public Function deSerializeObj(obj As Object, ByVal path As String) As Object
    
            Dim fs As FileStream = New FileStream(path, FileMode.Open)
            Dim xs As New XmlSerializer(obj.GetType)
            obj = CType(xs.Deserialize(fs), Object)
            fs.Close()
            Return obj
    
        End Function

    It runs but my datagrid view does not show any object information it is just blank. but i could count the objects, what am i doing wrong?

    If i do it like below it works but i want to know why the method above does not update the bindingsource?

    Dim ss As New BindingList(Of Project) ss = deSerializeObj(ProjectsList, fPath)

                    For Each project As Project In ss
                        ProjectsList.Add(project)
                    Next
    thanks

    Tuesday, October 13, 2020 1:47 PM

Answers

  • Does it work if you execute again ‘BS_ProjectsList.DataSource = ProjectsList’ and maybe ‘DataGridView4.DataSource = Nothing’ and ‘DataGridView4.DataSource = BS_ProjectsList’ after deserialization?

    • Marked as answer by Shan1986 Tuesday, October 13, 2020 8:09 PM
    Tuesday, October 13, 2020 7:07 PM

All replies

  • Does it work if you execute again ‘BS_ProjectsList.DataSource = ProjectsList’ and maybe ‘DataGridView4.DataSource = Nothing’ and ‘DataGridView4.DataSource = BS_ProjectsList’ after deserialization?

    • Marked as answer by Shan1986 Tuesday, October 13, 2020 8:09 PM
    Tuesday, October 13, 2020 7:07 PM
  • Does it work if you execute again ‘BS_ProjectsList.DataSource = ProjectsList’ and maybe ‘DataGridView4.DataSource = Nothing’ and ‘DataGridView4.DataSource = BS_ProjectsList’ after deserialization?

    Yes..BS_ProjectsList.DataSource = ProjectsList does work. Thanks 
    Tuesday, October 13, 2020 8:09 PM