我没有正确序列化窗体中的所有控件
我检查了fieldDataList
是否包含所有控件,但在xml文件中只得到一个条目
是什么原因导致列表中的所有项都没有序列化到xml文件中?
序列化的代码:
using System;
using System.Collections.Generic;
using System.IO;
using System.Windows.Forms;
using System.Xml;
using System.Xml.Serialization;
public class FieldData
{
public string ControlName { get; set; }
public string Value { get; set; }
}
public class MainForm : Form
{
private List<FieldData> fieldDataList;
public MainForm()
{
// Initialize the list to store field data
fieldDataList = new List<FieldData>();
// Add controls to the form
TextBox textBox1 = new TextBox();
textBox1.Name = "textBox1";
TextBox textBox2 = new TextBox();
textBox2.Name = "textBox2";
Button saveButton = new Button();
saveButton.Text = "Save";
saveButton.Click += SaveButton_Click;
Button loadButton = new Button();
loadButton.Text = "Load";
loadButton.Click += LoadButton_Click;
// Add controls to the form's controls collection
Controls.Add(textBox1);
Controls.Add(textBox2);
Controls.Add(saveButton);
Controls.Add(loadButton);
}
private void SaveButton_Click(object sender, EventArgs e)
{
// Collect field data from all controls on the form
CollectFieldData(Controls[0]);
// Save the field data to an XML file
string filePath = "field_data.xml";
SaveFieldDataToXml(filePath);
MessageBox.Show("Field data saved to XML file: " + filePath);
}
private void LoadButton_Click(object sender, EventArgs e)
{
// Load field data from an XML file
string filePath = "field_data.xml";
LoadFieldDataFromXml(filePath);
// Update the controls on the form with the loaded field data
UpdateFormControls();
MessageBox.Show("Field data loaded from XML file: " + filePath);
}
private void SaveFieldDataToXml(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<FieldData>));
using (StreamWriter writer = new StreamWriter(filePath))
{
serializer.Serialize(writer, fieldDataList);
}
}
private void LoadFieldDataFromXml(string filePath)
{
XmlSerializer serializer = new XmlSerializer(typeof(List<FieldData>));
using (StreamReader reader = new StreamReader(filePath))
{
fieldDataList = (List<FieldData>)serializer.Deserialize(reader);
}
}
private void CollectFieldData(Control parentControl)
{
foreach (Control control in parentControl.Controls)
{
if (control is TextBox textBox)
{
// Collect text box value
FieldData fieldData = new FieldData();
fieldData.ControlName = textBox.Name;
fieldData.Value = textBox.Text;
fieldDataList.Add(fieldData);
}
else if (control is NumericUpDown numericUpDown)
{
// Collect numeric up-down value
FieldData fieldData = new FieldData();
fieldData.ControlName = numericUpDown.Name;
fieldData.Value = numericUpDown.Value.ToString();
fieldDataList.Add(fieldData);
}
else if (control is CheckBox checkBox)
{
// Collect checkbox value
FieldData fieldData = new FieldData();
fieldData.ControlName = checkBox.Name;
fieldData.Value = checkBox.Checked.ToString();
fieldDataList.Add(fieldData);
}
else if (control.HasChildren)
{
// Recursively collect field data for child controls
CollectFieldData(control);
}
}
}
private void UpdateFormControls(Control parentControl)
{
foreach (Control control in parentControl.Controls)
{
if (control is TextBox textBox)
{
// Find the corresponding field data and update the text box value
FieldData fieldData = fieldDataList.Find(data => data.ControlName == textBox.Name);
if (fieldData != null)
{
textBox.Text = fieldData.Value;
}
}
else if (control is NumericUpDown numericUpDown)
{
// Find the corresponding field data and update the numeric up-down value
FieldData fieldData = fieldDataList.Find(data => data.ControlName == numericUpDown.Name);
if (fieldData != null)
{
if (decimal.TryParse(fieldData.Value, out decimal value))
{
numericUpDown.Value = value;
}
}
}
else if (control is CheckBox checkBox)
{
// Find the corresponding field data and update the checkbox value
FieldData fieldData = fieldDataList.Find(data => data.ControlName == checkBox.Name);
if (fieldData != null)
{
if (bool.TryParse(fieldData.Value, out bool value))
{
checkBox.Checked = value;
}
}
}
else if (control.HasChildren)
{
// Recursively update child controls
UpdateFormControls(control);
}
}
}
public static void Main()
{
Application.Run(new MainForm());
}
}
我得到的序列化文件的内容如下
<?xml version="1.0" encoding="utf-8"?><ArrayOfFieldData xmlns:xsd="http://www.w3.org/2001/XMLSchema" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" />
1条答案
按热度按时间fcy6dtqo1#
您在SaveButton_Click方法中编写了“从表单上的所有控件收集字段数据”。所以应该是
那么你的方法调用应该是这样的,因为你传递的是ControlCollection
else if块中的最后一行应该是
希望这有帮助!