//更新我已经添加了更正并提供了完整的 Form1.c 代码。
为了简单起见,我将其简化了一点。所以我所有的表单控件都设置好了。为了这个问题,日期为 DateTimePicker(Short),员工编号为 maskedTextBox("###-##"),员工姓名为简单文本框,SS 编号为 maskedTextBox(标准 SSNum) 等。为了处理所有这些数据,我创建了一个名为 Employee_Data 的类,如下所示...
namespace Employee
{
public class Employee_Data
{
//Make Members Xml elements
[XmlElement("EmployeeNumber")]
public String EmployeeNumber {get; set;}
[XmlElement("Date")]
public DateTime Date {get; set;}
[XmlElement("EmployeeName")]
public String EmployeeName {get; set;}
[XmlElement("SSNumber")]
public String SSNumber {get; set;}
}
}
我在 Form1.cs 中创建了一个名为 e_d 的对象,并在 VS 中使用了自动事件处理。调试器显示这些值被很好地存储到类对象中(maskedText 值包括破折号)。但是,一旦我尝试如下所示序列化数据,则 xml 文件中将省略屏蔽的 textValues ...
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Xml.Serialization;
using System.IO;
using System.Xml;
namespace Employee
{
public partial class Form1 : Form
{
public Form1()
{
InitializeComponent();
}
Employee_Data e_d = new Employee_Data();
private void EmployyeeNumber_TextChanged(object sender, EventArgs e)
{
e_d.EmployeeNumber=EmployeeNumber.Text;
}
private void Date_ValueChanged(object sender, EventArgs e)
{
e_d.Date=Date.Value;
}
private void EmployeeName_TextChanged(object sender, EventArgs e)
{
e_d.EmployeeName=EmployeeName.Text;
}
private void SSNumber_ValueChanged(object sender, EventArgs e)
{
e_d.SSNumber=SSNumber.Text;
}
private void saveButton_Click(object sender, EventArgs e)
{
saveFileDialog1.DefaultExt = "xml";
saveFileDialog1.Filter = "XML File (.xml)|*.xml";
saveFileDialog1.AddExtension = true;
saveFileDialog1.RestoreDirectory = true;
saveFileDialog1.ShowDialog();
}
private void saveFileDialog1_FileOk(object sender, CancelEventArgs e)
{
string name = saveFileDialog1.FileName;
using (TextWriter texWrt = new StreamWriter(saveFileDialog1.FileName))
{
XmlSerializer serializer = new XmlSerializer(typeof(Employee_Data));
serializer.Serialize(texWrt, e_d);
}
}
我已经研究过这个问题,关于这个特定主题似乎没有太多。