Deserialization

This is very aggrevating!!! I have serialized three simple questions (textboxes) that is entered by the user. I cannot figure out how deserialize and have the three textboxes show what I have serialized My code (Visual Basic Express) on my main form is as follows:

Imports System.Xml

Imports System.Xml.Serialization

Imports System.IO

Public Class Form1

Private theCollectionOfPatients As New ArrayList()

Private Sub DoSerializePatient(ByVal patientcollection As ArrayList)

Dim theTypes(1) As Type

theTypes(0) = GetType(ArrayList)

theTypes(1) = GetType(Patient)

Dim theSerializer As New XmlSerializer(GetType(ArrayList), theTypes)

Dim theWriter As New FileStream("AllPatients.xml", FileMode.Create)

theSerializer.Serialize(theWriter, Me.theCollectionOfPatients)

theWriter.Close()

End Sub

Private Sub Form1_Load(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles MyBase.Load

End Sub

Private Sub TextBox1_TextChanged(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles TextBox1.TextChanged

End Sub

Private Sub DoDeserializePatient()

Dim theTypes(1) As Type

theTypes(0) = GetType(ArrayList)

theTypes(1) = GetType(Patient)

Dim theSerializer As New XmlSerializer(GetType(ArrayList), theTypes)

Dim theReader As New FileStream("AllPatients.xml", FileMode.Open)

Me.theCollectionOfPatients = CType(theSerializer.Deserialize(theReader), ArrayList)

theReader.Close()

End Sub

Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

'this is the button to open file

Me.DoDeserializePatient()

End Sub

Private Sub Button1_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button1.Click

Dim theNewPatient As New Patient(TextBox1.Text)

theNewPatient.PatientAddress = TextBox2.Text

theNewPatient.PatientPhoneNumber = TextBox3.Text

Me.DoSerializePatient(Me.theCollectionOfPatients)

End Sub

End Class



Answer this question

Deserialization

  • RAMPRAKASH

    You have been a huge help to me and I thank you very much. I'm going to try and figure out how to now delete an array from which I have serialized. But I want to try and figure it out myself if I can. Thank you.
  • David N.4117

    I think I understand the concept. Tell me if I'm on the right track. When I deserialize the data, the application then has the data i.e. name, address on hand for me to assign an action to that data. In other words I must deserialize the data and then assign a specific action for each individual data such as textbox1.text = patient's name. My question now is, how do I go through each element in the array list How do I identify specific data in the 'theCollectionOfPatients' to be put placed where I want it i.e. textbox1 Will you please look at the coding below and tell me what I'm not getting I feel as though I'm missing the coding to read the arraylist.. .

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'this is the button to open file

    Me.DoDeserializePatient()

    TextBox1.Text = Patient

    TextBox2.Text = PatientAddress

    TextBox3.Text = PatientPhoneNumber

    End Sub


  • Prabu.

    I am getting "Name 'currentpatient' not declared" in the private sub. Why I thought it was declared in the "For Each currentPatient" statement.

    Private Sub Button2_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button2.Click

    'this is the button to open file

    Me.DoDeserializePatient()

    For Each currentPatient As Patient In Me.theCollectionOfPatients

    Next

    MessageBox.Show(currentPatient.Name & Environment.NewLine & currentPatient.Age.ToString() & Environment.NewLine & currentPatient.Address)

    Me.ComboBox1.Items.Add(currentPatient.Name)

    End Sub

    I had also considered SQL and MS Access database. I will not have the capabilities of utilizing a database server. However, do you think MS Access would be more appropriate. This is what I'm trying to do... I want to build an application that can provide a form for input by the user for numerous patients. Then take the information entered on the main form and apply it to several other forms. For example, a patient's name is on a physician order, medication administration record, consents, delegation orders, IDT staffing, ect. I want to enter this information one time and then pull up any number of forms and the information be instantly in place.


  • Garry W

    :-)

    well, each has their own difficulties. It shouldnt be that hard to learn - I would say about the same. MS Access database will be used if the database is stored in the same directory (for example, or somewhere on the computer) as the application. If it doesnt exist, you can't connect to it, or do anything related to databases. This is where SQL Server comes in handy - one central location, anyone from anywhere can access it - better managed since MS Access for really for mini type databases and not an industry type application like SQL is.

    you can use SQL Express which is free.

    you could maybe connect to the MS Access database from any location probably, if its stored in one central location but the number of connections would probably be limited, unlike SQL Server

     



  • GrayTap Media

    anytime, glad we could help! I would also suggest, perhaps something in the background, to do some investigating in using a proper database engine - MS Access or SQL and implement the solution using that.

  • mikalush

    almost there. Remember, if there are 3 patients and you go through each patient and setting the textbox's text to show say, the patient's name, age, address - then the last patient will be the one the textbox will be showing the data for.

    First thing is first. Let's go through each element of the array and show the patient info. So AFTER deserializing, you could place this code or perhaps on some other button event, once you have Deserialized the data.

     

    for each currentPatient as Patient in Me.theCollectionOfPatients

       MessageBox.Show(currentPatient.Name & Environment.NewLine & currentPatient.Age.ToString() & Environment.NewLine & currentPatient.Address)

    next

     

    this will go through each item in the array, which have the patients which have been deserialized (loaded from file) and show you the details of the patient in a messagebox.

     

    now, to show the specific patient details in the textbox you have 2 options:

     

  • know the exact location in the array of that patient - bad idea, its impossible for you to know of by heart/mind at what position in the array that patient object will be stored in

  • have a drop down list with the names of each patient perhaps, or something like this (perhaps a record ID...whatever) then depending on the selection, show the details - better Idea.

     

    so...once deserialized, you may wish to populate the patient's name, for example, in a combobox:

    ...

    for each currentPatient as Patient in Me.theCollectionOfPatients

       Me.theComboBox.Items.Add(currentPatient.Name)

    next

     

    when you select the combobox, you want to get their details and put it in a textbox correct So, double click the combobox to make a selectedindexchanged event handler then do this:

     

    if Me.theCollectionOfObjects.Count > 0 then

       Dim thePatient as Patient = CType(Me.theCollectionOfPatients(Me.theComboBox.SelectedIndex), Patient) 'converts the object in the array to a patient. You may be better of using a Generic/List collection but this is something for later for you to investigate in

       Me.theTextBoxName.Text = thePatient.Name

       Me.theTextBoxAge.Text = thePatient.Age

       Me.theTextBoxAddress.Text = thePatient.Address

    end if

     

    Now, since patients may have same names, its hard for you to know who is who without checking each same patient name in the list correct Each patient has an ID number of some sort correct

    So now, you would need to add a property in the Patient class called "ID" (same way the other properties were created such as name, address etc...) and make sure you assign an ID to the patient. Then we can populate the combobox with the Patient ID's so you can type in the ID number and press enter, or an OK button to bring the correct patient details by going through each element of the array and checking the current patient's ID number - similar to the above

     

    since now this is going towards more of a database approach, you may wish to consider using SQL Server or MS Access since it is better managable, in a sense, such as assigning unique ID numbers and so on, and querying the tables and getting back the results correctly, then binding them to a bindable UI component, such as a datagridvew or a textbox

     

    does this help/make some sense



  • AbelMorelos

    I appreciate this. I promise I have read all of the material you provided me. I am not just trying to find a quick fix to my programming mess or try to get you to do my work for me. I truely want to learn this, but it seems as though I'm just not grasping it. I will try to work on the information you just gave me. Thanks again.
  • lkshikoh

    you are not deleting a file there, you are clearing out the collection of patients stored in the combobox items. A quicker way to clear out the contents of the items in the combobox would be:

    Me.ComboBox.Items.Clear()

    to delete a file:

    System.IO.File.Delete(pathToFile\FileName.ext)



  • J Hallam

    That make sense. Am I on the right track and if so, why isn't it deleting the file

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    'delete a file

    For Each currentPatient As Patient In Me.theCollectionOfPatients

    System.IO.File.Delete(Me.theCollectionOfPatients(currentPatient.PatientName))

    Next

    End Sub


  • db_olap

    Why doesn't this code work to delete a file that has been serialized

    Private Sub Button3_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles Button3.Click

    'delete a file

    For Each currentPatient As Patient In Me.theCollectionOfPatients

    Me.ComboBox1.Items.Remove(currentPatient)

    Next

    End Sub


  • wvsever

    not at all! Please DO ask questions, its the only way to learn - there are many ways of explaining a problem, each way perhaps better than the previous :-) Please do ask, we will be more than happy to help out

  • monkeyMaciej

    since this is a continuation of what we discussed last time - thought I would pop in!

    the items are being deserialized to the arraylist - Me.theCollectionOfPatients. you would go through each element in the array list to get a Patient object then do whatever it is you like with the object, such as getting the properties like name, age, address etc... and showing it in the textbox.

    currently you are assigning values from a textbox into theNewPatient object but its not doing anything.....

    once the object (patient object) as been created, you need to add this to the collection of patients:

    Me.theCollectionOfPatients.Add(theNewPatient)

    then serialize once again if you need to store the items to the xml file

    To serialize, look at the DoSerializePatient. As you can see it will serialize ALL the items in the Me.theCollectionOfPatients, to the xml file.

     

    this is all these methods do at the moment. Does this help explain things better What else would you like the app to do



  • Christian Knoepfle

    sure. MS Access would be your best bet to be honest. So you need to create tables, relationships too, read up on the forums about using MS Access, the internet articles too! And of course ask questions here.

    as for the error, this is because you are using a variable outside of the for each loop - its out of its scope.

    it should be in the for loop:

    For Each currentPatient As Patient In Me.theCollectionOfPatients

    Me.ComboBox1.Items.Add(currentPatient.Name)

    MessageBox.Show(currentPatient.Name & Environment.NewLine & currentPatient.Age.ToString() & Environment.NewLine & currentPatient.Address)

    Next

    hope this helps!

    Definately you need to use a database engine which can provide you better in the long run. MS Access, or SQL, being the way to go. You need to read up on OleDb, OleDbCommand, DataSets, OleDbDataAdapter, perhaps also the DataGridView control to bind your data and so on.



  • Sajid Hussain

    You did it! Is the use of a database engine more difficult to learn than serializing (you can probably tell I had a pretty difficult time with this). But if I did use a database enging, would it be usable if the computer I put the saved application on is not online
  • Deserialization