【问题标题】:Restore details from xml file into winform将xml文件中的详细信息还原到winform
【发布时间】:2013-02-16 20:01:25
【问题描述】:


这里我通过序列化将详细信息从 form1 保存到 data.xml。

现在,我想通过在表格 2 中搜索患者 ID 来获取详细信息,并在单击表格 2 中的恢复按钮时恢复到表格 1 的文本框中。

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();   }

坦率地说,我没有尝试恢复数据,我不知道 xml 是大学的 ma 项目。请帮助我学习。现在我的问题是我想通过在 form2 中搜索患者 ID 并在 form1 中显示来从 PatientD.xml 恢复数据

注意:一个项目有两种形式

【问题讨论】:

  • 好的,你已经说了你想要达到的目标。不过,您还没有提出问题,也没有展示您迄今为止尝试过的内容。请阅读tinyurl.com/so-listtinyurl.com/so-hints
  • 不确定您到底想做什么?这些是单独的程序吗?我假设当您按下保存时,您会保存到 XML 输出。然后在该表单2中,当您按下恢复时,您可以选择 XML 文件并导入数据。我对么?另外,您使用 ISerializable 还是 IXmlSerializable?
  • 至少表格看起来漂亮漂亮..lol where is the code that you are using on the Save as well as the Restore button Click

标签: c# xml winforms visual-studio xml-deserialization


【解决方案1】:

听起来你已经成功了一半。 data.xml 是否包含您的患者详细信息列表,还是序列化为单独文件的单个实例?这是一个重要的问题,因为它将控制您需要如何反序列化。

我假设您有一个序列化为 data.xml 的患者详细信息对象列表。所以你可能有这样的课程:

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

根据您的描述,您的 Form2 可能是一个模型输入框,当您单击加载时会打开。您应该在 Form2 类上创建一个名为 PatientID 的公共属性。

当用户输入数字并点击恢复时,您可以设置 PatientID 属性并关闭 Form2 输入框。返回 Form1 类,从 Form2 实例上的 PatientID 检索值。这将在您反序列化数据时使用。

再次,假设您已将 PatientDetail 对象列表序列化为数据,您将需要反序列化 XML 文件并找到所需的实例:

//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();

现在您拥有反序列化列表中的实例,您将能够填充表单上的文本框。我显然没有在这里进行任何验证(例如检查文件是否存在),将数据检索与接口逻辑分开是个好主意。例如。存储库类

【讨论】:

    猜你喜欢
    • 2022-01-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-12-07
    相关资源
    最近更新 更多