using
System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace EmployeeData
... {
/**//// <summary>
/// Summary description for DataForm.
/// </summary>
public class DataForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtDepartment;
private System.Windows.Forms.TextBox txtEmail;
private System.Windows.Forms.TextBox txtAge;
private System.Windows.Forms.TextBox txtStartDate;
private System.Windows.Forms.TextBox txtCurrency;
private System.Windows.Forms.TextBox txtAmount;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnCancel;
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DataForm()
...{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
this.txtName = new System.Windows.Forms.TextBox();
this.txtDepartment = new System.Windows.Forms.TextBox();
this.txtEmail = new System.Windows.Forms.TextBox();
this.txtCurrency = new System.Windows.Forms.TextBox();
this.txtAmount = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtStartDate = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.txtAge = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.btnSave = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(88, 24);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(176, 22);
this.txtName.TabIndex = 0;
this.txtName.Text = "";
//
// txtDepartment
//
this.txtDepartment.Location = new System.Drawing.Point(88, 56);
this.txtDepartment.Name = "txtDepartment";
this.txtDepartment.Size = new System.Drawing.Size(176, 22);
this.txtDepartment.TabIndex = 1;
this.txtDepartment.Text = "";
//
// txtEmail
//
this.txtEmail.Location = new System.Drawing.Point(88, 88);
this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(176, 22);
this.txtEmail.TabIndex = 2;
this.txtEmail.Text = "";
//
// txtCurrency
//
this.txtCurrency.Location = new System.Drawing.Point(88, 24);
this.txtCurrency.MaxLength = 3;
this.txtCurrency.Name = "txtCurrency";
this.txtCurrency.Size = new System.Drawing.Size(48, 22);
this.txtCurrency.TabIndex = 3;
this.txtCurrency.Text = "";
//
// txtAmount
//
this.txtAmount.Location = new System.Drawing.Point(88, 56);
this.txtAmount.Name = "txtAmount";
this.txtAmount.Size = new System.Drawing.Size(88, 22);
this.txtAmount.TabIndex = 4;
this.txtAmount.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Name:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 6;
this.label2.Text = "Department:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 7;
this.label3.Text = "Email:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 8;
this.label4.Text = "Currency:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 56);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 23);
this.label5.TabIndex = 9;
this.label5.Text = "Amount:";
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.txtStartDate,
this.label7,
this.label6,
this.txtAge,
this.txtName,
this.txtEmail,
this.label3,
this.txtDepartment,
this.label1,
this.label2});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 184);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Personal Info";
//
// txtStartDate
//
this.txtStartDate.Location = new System.Drawing.Point(88, 152);
this.txtStartDate.Name = "txtStartDate";
this.txtStartDate.TabIndex = 11;
this.txtStartDate.Text = "mm/dd/yyyy";
//
// label7
//
this.label7.Location = new System.Drawing.Point(8, 152);
this.label7.Name = "label7";
this.label7.TabIndex = 10;
this.label7.Text = "Start Date:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 120);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 9;
this.label6.Text = "Age:";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(88, 120);
this.txtAge.MaxLength = 2;
this.txtAge.Name = "txtAge";
this.txtAge.TabIndex = 8;
this.txtAge.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.label4,
this.label5,
this.txtCurrency,
this.txtAmount});
this.groupBox2.Location = new System.Drawing.Point(8, 200);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(280, 88);
this.groupBox2.TabIndex = 11;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Salary";
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(128, 296);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 12;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(208, 296);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 13;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// DataForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(296, 328);
this.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.btnCancel,
this.btnSave,
this.groupBox2,
this.groupBox1});
this.Name = "DataForm";
this.Text = "Employee Data";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new DataForm());
}
private void btnSave_Click(object sender, System.EventArgs e) ...{
// Create new Employee object
Employee NewEmployee = new Employee();
// Save personal info to Employee object
NewEmployee.Name = txtName.Text;
NewEmployee.Department = txtDepartment.Text;
NewEmployee.Email = txtEmail.Text;
NewEmployee.Age = Convert.ToInt32(txtAge.Text);
NewEmployee.StartDate = Convert.ToDateTime(txtStartDate.Text);
// Save salary info to new Money object under Employee object
NewEmployee.Salary = new Money();
NewEmployee.Salary.Currency = txtCurrency.Text;
NewEmployee.Salary.Amount = Convert.ToDecimal(txtAmount.Text);
// Serialize object and display XML
string strEmployeeData = SerializeObject(NewEmployee);
MessageBox.Show(strEmployeeData);
//Deserialize XML
DeserializeXml(strEmployeeData);
}
private string SerializeObject(Employee EmpObject) ...{
// Create new XmlSerializer object
XmlSerializer MySerializer = new XmlSerializer(typeof(Employee));
// Create new StringWriter to hold serialized XML
TextWriter TW = new StringWriter();
// Serialize Employee object
MySerializer.Serialize(TW, EmpObject);
// Return XML content of StringWriter
return TW.ToString();
}
private void DeserializeXml(string XmlData) ...{
// Create new Employee object
Employee ReturnedEmployee;
// Create new XmlSerializer
XmlSerializer MyDeserializer = new XmlSerializer(typeof(Employee));
// Create StringReader to pass XML data
TextReader TR = new StringReader(XmlData);
XmlReader XR = new XmlTextReader(TR);
// Check if object can be deserialized
if (MyDeserializer.CanDeserialize(XR)) ...{
// Deserialize XML
ReturnedEmployee = (Employee)MyDeserializer.Deserialize(XR);
//ShowEmployeeData(ReturnedEmployee);
}
}
private void ShowEmployeeData(Employee EmpObject) ...{
// Create new form to show deserialized data
ReturnedData ReturnForm = new ReturnedData();
// Construct output string from deserialized data
StringBuilder OutputString = new StringBuilder("Employee Information: ");
OutputString.Append("Name: " + EmpObject.Name + " ");
OutputString.Append("Department: " + EmpObject.Department + " ");
OutputString.Append("Email: " + EmpObject.Email + " ");
OutputString.Append("Age: " + EmpObject.Age.ToString() + " ");
OutputString.Append("Start Date: " + EmpObject.StartDate.ToString() + " ");
OutputString.Append("Salary: " + EmpObject.Salary.Currency + EmpObject.Salary.Amount + " ");
ReturnForm.txtEmployeeData.Text = OutputString.ToString();
ReturnForm.ShowDialog();
}
private void btnCancel_Click(object sender, System.EventArgs e) ...{
this.Dispose();
}
}
}
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.IO;
using System.Text;
using System.Xml;
using System.Xml.Serialization;
namespace EmployeeData
... {
/**//// <summary>
/// Summary description for DataForm.
/// </summary>
public class DataForm : System.Windows.Forms.Form
...{
private System.Windows.Forms.GroupBox groupBox1;
private System.Windows.Forms.GroupBox groupBox2;
private System.Windows.Forms.Label label1;
private System.Windows.Forms.Label label2;
private System.Windows.Forms.Label label3;
private System.Windows.Forms.Label label4;
private System.Windows.Forms.Label label5;
private System.Windows.Forms.Label label6;
private System.Windows.Forms.Label label7;
private System.Windows.Forms.TextBox txtName;
private System.Windows.Forms.TextBox txtDepartment;
private System.Windows.Forms.TextBox txtEmail;
private System.Windows.Forms.TextBox txtAge;
private System.Windows.Forms.TextBox txtStartDate;
private System.Windows.Forms.TextBox txtCurrency;
private System.Windows.Forms.TextBox txtAmount;
private System.Windows.Forms.Button btnSave;
private System.Windows.Forms.Button btnCancel;
/**//// <summary>
/// Required designer variable.
/// </summary>
private System.ComponentModel.Container components = null;
public DataForm()
...{
//
// Required for Windows Form Designer support
//
InitializeComponent();
//
// TODO: Add any constructor code after InitializeComponent call
//
}
/**//// <summary>
/// Clean up any resources being used.
/// </summary>
protected override void Dispose( bool disposing )
...{
if( disposing )
...{
if (components != null)
...{
components.Dispose();
}
}
base.Dispose( disposing );
}
Windows Form Designer generated code#region Windows Form Designer generated code
/**//// <summary>
/// Required method for Designer support - do not modify
/// the contents of this method with the code editor.
/// </summary>
private void InitializeComponent()
...{
this.txtName = new System.Windows.Forms.TextBox();
this.txtDepartment = new System.Windows.Forms.TextBox();
this.txtEmail = new System.Windows.Forms.TextBox();
this.txtCurrency = new System.Windows.Forms.TextBox();
this.txtAmount = new System.Windows.Forms.TextBox();
this.label1 = new System.Windows.Forms.Label();
this.label2 = new System.Windows.Forms.Label();
this.label3 = new System.Windows.Forms.Label();
this.label4 = new System.Windows.Forms.Label();
this.label5 = new System.Windows.Forms.Label();
this.groupBox1 = new System.Windows.Forms.GroupBox();
this.txtStartDate = new System.Windows.Forms.TextBox();
this.label7 = new System.Windows.Forms.Label();
this.label6 = new System.Windows.Forms.Label();
this.txtAge = new System.Windows.Forms.TextBox();
this.groupBox2 = new System.Windows.Forms.GroupBox();
this.btnSave = new System.Windows.Forms.Button();
this.btnCancel = new System.Windows.Forms.Button();
this.groupBox1.SuspendLayout();
this.groupBox2.SuspendLayout();
this.SuspendLayout();
//
// txtName
//
this.txtName.Location = new System.Drawing.Point(88, 24);
this.txtName.Name = "txtName";
this.txtName.Size = new System.Drawing.Size(176, 22);
this.txtName.TabIndex = 0;
this.txtName.Text = "";
//
// txtDepartment
//
this.txtDepartment.Location = new System.Drawing.Point(88, 56);
this.txtDepartment.Name = "txtDepartment";
this.txtDepartment.Size = new System.Drawing.Size(176, 22);
this.txtDepartment.TabIndex = 1;
this.txtDepartment.Text = "";
//
// txtEmail
//
this.txtEmail.Location = new System.Drawing.Point(88, 88);
this.txtEmail.Name = "txtEmail";
this.txtEmail.Size = new System.Drawing.Size(176, 22);
this.txtEmail.TabIndex = 2;
this.txtEmail.Text = "";
//
// txtCurrency
//
this.txtCurrency.Location = new System.Drawing.Point(88, 24);
this.txtCurrency.MaxLength = 3;
this.txtCurrency.Name = "txtCurrency";
this.txtCurrency.Size = new System.Drawing.Size(48, 22);
this.txtCurrency.TabIndex = 3;
this.txtCurrency.Text = "";
//
// txtAmount
//
this.txtAmount.Location = new System.Drawing.Point(88, 56);
this.txtAmount.Name = "txtAmount";
this.txtAmount.Size = new System.Drawing.Size(88, 22);
this.txtAmount.TabIndex = 4;
this.txtAmount.Text = "";
//
// label1
//
this.label1.Location = new System.Drawing.Point(8, 24);
this.label1.Name = "label1";
this.label1.Size = new System.Drawing.Size(48, 23);
this.label1.TabIndex = 5;
this.label1.Text = "Name:";
//
// label2
//
this.label2.Location = new System.Drawing.Point(8, 56);
this.label2.Name = "label2";
this.label2.Size = new System.Drawing.Size(80, 23);
this.label2.TabIndex = 6;
this.label2.Text = "Department:";
//
// label3
//
this.label3.Location = new System.Drawing.Point(8, 88);
this.label3.Name = "label3";
this.label3.Size = new System.Drawing.Size(48, 23);
this.label3.TabIndex = 7;
this.label3.Text = "Email:";
//
// label4
//
this.label4.Location = new System.Drawing.Point(8, 24);
this.label4.Name = "label4";
this.label4.Size = new System.Drawing.Size(72, 23);
this.label4.TabIndex = 8;
this.label4.Text = "Currency:";
//
// label5
//
this.label5.Location = new System.Drawing.Point(8, 56);
this.label5.Name = "label5";
this.label5.Size = new System.Drawing.Size(64, 23);
this.label5.TabIndex = 9;
this.label5.Text = "Amount:";
//
// groupBox1
//
this.groupBox1.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.txtStartDate,
this.label7,
this.label6,
this.txtAge,
this.txtName,
this.txtEmail,
this.label3,
this.txtDepartment,
this.label1,
this.label2});
this.groupBox1.Location = new System.Drawing.Point(8, 8);
this.groupBox1.Name = "groupBox1";
this.groupBox1.Size = new System.Drawing.Size(280, 184);
this.groupBox1.TabIndex = 10;
this.groupBox1.TabStop = false;
this.groupBox1.Text = "Personal Info";
//
// txtStartDate
//
this.txtStartDate.Location = new System.Drawing.Point(88, 152);
this.txtStartDate.Name = "txtStartDate";
this.txtStartDate.TabIndex = 11;
this.txtStartDate.Text = "mm/dd/yyyy";
//
// label7
//
this.label7.Location = new System.Drawing.Point(8, 152);
this.label7.Name = "label7";
this.label7.TabIndex = 10;
this.label7.Text = "Start Date:";
//
// label6
//
this.label6.Location = new System.Drawing.Point(8, 120);
this.label6.Name = "label6";
this.label6.Size = new System.Drawing.Size(48, 23);
this.label6.TabIndex = 9;
this.label6.Text = "Age:";
//
// txtAge
//
this.txtAge.Location = new System.Drawing.Point(88, 120);
this.txtAge.MaxLength = 2;
this.txtAge.Name = "txtAge";
this.txtAge.TabIndex = 8;
this.txtAge.Text = "";
//
// groupBox2
//
this.groupBox2.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.label4,
this.label5,
this.txtCurrency,
this.txtAmount});
this.groupBox2.Location = new System.Drawing.Point(8, 200);
this.groupBox2.Name = "groupBox2";
this.groupBox2.Size = new System.Drawing.Size(280, 88);
this.groupBox2.TabIndex = 11;
this.groupBox2.TabStop = false;
this.groupBox2.Text = "Salary";
//
// btnSave
//
this.btnSave.Location = new System.Drawing.Point(128, 296);
this.btnSave.Name = "btnSave";
this.btnSave.TabIndex = 12;
this.btnSave.Text = "Save";
this.btnSave.Click += new System.EventHandler(this.btnSave_Click);
//
// btnCancel
//
this.btnCancel.Location = new System.Drawing.Point(208, 296);
this.btnCancel.Name = "btnCancel";
this.btnCancel.TabIndex = 13;
this.btnCancel.Text = "Cancel";
this.btnCancel.Click += new System.EventHandler(this.btnCancel_Click);
//
// DataForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 15);
this.ClientSize = new System.Drawing.Size(296, 328);
this.Controls.AddRange(new System.Windows.Forms.Control[] ...{
this.btnCancel,
this.btnSave,
this.groupBox2,
this.groupBox1});
this.Name = "DataForm";
this.Text = "Employee Data";
this.groupBox1.ResumeLayout(false);
this.groupBox2.ResumeLayout(false);
this.ResumeLayout(false);
}
#endregion
/**//// <summary>
/// The main entry point for the application.
/// </summary>
[STAThread]
static void Main()
...{
Application.Run(new DataForm());
}
private void btnSave_Click(object sender, System.EventArgs e) ...{
// Create new Employee object
Employee NewEmployee = new Employee();
// Save personal info to Employee object
NewEmployee.Name = txtName.Text;
NewEmployee.Department = txtDepartment.Text;
NewEmployee.Email = txtEmail.Text;
NewEmployee.Age = Convert.ToInt32(txtAge.Text);
NewEmployee.StartDate = Convert.ToDateTime(txtStartDate.Text);
// Save salary info to new Money object under Employee object
NewEmployee.Salary = new Money();
NewEmployee.Salary.Currency = txtCurrency.Text;
NewEmployee.Salary.Amount = Convert.ToDecimal(txtAmount.Text);
// Serialize object and display XML
string strEmployeeData = SerializeObject(NewEmployee);
MessageBox.Show(strEmployeeData);
//Deserialize XML
DeserializeXml(strEmployeeData);
}
private string SerializeObject(Employee EmpObject) ...{
// Create new XmlSerializer object
XmlSerializer MySerializer = new XmlSerializer(typeof(Employee));
// Create new StringWriter to hold serialized XML
TextWriter TW = new StringWriter();
// Serialize Employee object
MySerializer.Serialize(TW, EmpObject);
// Return XML content of StringWriter
return TW.ToString();
}
private void DeserializeXml(string XmlData) ...{
// Create new Employee object
Employee ReturnedEmployee;
// Create new XmlSerializer
XmlSerializer MyDeserializer = new XmlSerializer(typeof(Employee));
// Create StringReader to pass XML data
TextReader TR = new StringReader(XmlData);
XmlReader XR = new XmlTextReader(TR);
// Check if object can be deserialized
if (MyDeserializer.CanDeserialize(XR)) ...{
// Deserialize XML
ReturnedEmployee = (Employee)MyDeserializer.Deserialize(XR);
//ShowEmployeeData(ReturnedEmployee);
}
}
private void ShowEmployeeData(Employee EmpObject) ...{
// Create new form to show deserialized data
ReturnedData ReturnForm = new ReturnedData();
// Construct output string from deserialized data
StringBuilder OutputString = new StringBuilder("Employee Information: ");
OutputString.Append("Name: " + EmpObject.Name + " ");
OutputString.Append("Department: " + EmpObject.Department + " ");
OutputString.Append("Email: " + EmpObject.Email + " ");
OutputString.Append("Age: " + EmpObject.Age.ToString() + " ");
OutputString.Append("Start Date: " + EmpObject.StartDate.ToString() + " ");
OutputString.Append("Salary: " + EmpObject.Salary.Currency + EmpObject.Salary.Amount + " ");
ReturnForm.txtEmployeeData.Text = OutputString.ToString();
ReturnForm.ShowDialog();
}
private void btnCancel_Click(object sender, System.EventArgs e) ...{
this.Dispose();
}
}
}