//更新 我添加了更正并提供了Form1.c代码。
为了简单起见,我把它简化了一下。所以我的所有表单控件都已设置好。 Date为DateTimePicker(Short),Employee Number为maskedTextBox(“### - ##”),Employee Name为simple textBox,SS Number为maskedTextBox(标准SSNum)等,为了这个问题。为了处理所有这些数据,我创建了一个名为Employee_Data的类,如Follows ...
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);
}
}
我已经研究过这个问题,但这个问题似乎并不多。
答案 0 :(得分:0)
答案是事件处理或PROGRAMMER FAILURE。当程序员手动编码文本输入的事件处理时(因为VS中的默认值是MaskInputRejected),他忘了设置事件处理程序。我会确保和程序员讨论这件事,每个人。但是他在这个过程中学到了很多关于csharp和xml的知识。感谢所有人花时间提供帮助。