Sarath Barani
Sarath Barani

Reputation: 41

Restore details from xml file into winform

enter image description here enter image description here
Here I am saving details from form1 to data.xml by serializing.

Now, I want details by searching the patient ID in Form 2 and to restore into the textboxes of Form1 when I click the Restore button in Form2.

public class PatientData    {    public long Patient_ID;    public string Name;    public string Address;    public long Mobile;     }    private void Patient_clear()    {    Patient_ID.Text = "";    Mobile.Text = "";    Address.Text = "";    Name.Text = "";    }    private List<PatientData> GetPatients(string filename)   {    if (!File.Exists(filename))    return new List<PatientData>();    XmlSerializer xs = new XmlSerializer(typeof(List<PatientData>));    using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))    return (List<PatientData>)xs.Deserialize(fs);   }    public void SavePatients(string filename, List<PatientData> Patients)    {    XmlSerializer xs = new XmlSerializer(typeof(List<PatientData>));   using (FileStream fs = new FileStream(filename, FileMode.OpenOrCreate))    xs.Serialize(fs, Patients);   }   private void load_Click(object sender, EventArgs e)   {   System.Windows.Forms.Form Form2 = new Form2();   Form2.Show();   }   private void save_Click(object sender, EventArgs e)   {   List<PatientData> Patients = GetPatients(@"D:\PatientD.xml");   PatientData patient = new PatientData();   patient.Patient_ID = Patient_ID.MaxLength;   patient.Name = Name.Text;   patient.Address = Address.Text;   patient.Mobile = Mobile.MaxLength;   Patients.Add(patient);   SavePatients(@"D:\Sarath\Project\XML\curarisd\PatientD.xml", Patients);    MessageBox.Show("Inserted");   Patient_clear();   }

I haven't tried to restore data frankly i don't know xml its ma project for college.. Please help me to learn. Now my question is i want to restore the data from PatientD.xml by searching Patient ID in the form2 and display it in form1

Note: Its a one project with two forms

Upvotes: 0

Views: 360

Answers (1)

Bill
Bill

Reputation: 815

It sounds like you're halfway there. Does data.xml contain a list of your patient details or is it single instances serialised into separate files? This is an important question because it'll control how you need to deserialise.

I'm assuming that you have a list of patient details objects which is serialised to data.xml. so you probably have a class like this:

public class PatientDetail
{
    public string PatientID { get; set; }
    public string Name { get; set; }
    public string Address { get; set; }
    public string Mobile { get; set; }
}

Based on your description, your Form2 is probably a model input box that opens when you click on Load. You should create a public property on the Form2 class called PatientID.

When the user enters a number and hits restore you can set the PatientID property and close the Form2 input box. Back in the Form1 class, retrieve the value from PatientID on your Form2 instance. This will be used when you have deserialised the data.

Again, assuming you have serialised a List of PatientDetail objects as your data, you will need to deserialise the XML file and find the instance you want:

//Deserialise the file
XmlSerializer serialiser = new XmlSerializer(typeof(List<PatientDetail>));
StreamReader reader = new StreamReader("Data.xml");
List<PatientDetail> details = (List<PatientDetail>)serialiser.Deserialize(reader);
reader.Close();

//Find the record which matches the ID retrieved earlier from Form2
PatientDetail detail = details.Where(d => d.PatientID = patientID).First();

Now you have the instance from the deserialised list you'll be able to populate the text boxes on your form. I've obviously not put any validation on here (e.g. checking file exists), and it would be a good idea to separate your data retrieval away from your interface logic. E.g. a repository class

Upvotes: 1

Related Questions