一个书上简单的ERS.

只有一个窗体,EmployeeRecordsForm.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;

namespace Employee_Records_System
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class EmployeeRecordsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;

private TreeNode tvRootNode;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;

public EmployeeRecordsForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
PopulateTreeView();
initializeListControl();

//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}

/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}

#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.listView1 = new System.Windows.Forms.ListView();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(208, 352);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 359);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(720, 22);
this.statusBar1.TabIndex = 1;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "Click the employee code to view detials";
this.statusBarPanel1.Width = 400;
//
// listView1
//
this.listView1.Activation = System.Windows.Forms.ItemActivation.TwoClick;
this.listView1.Location = new System.Drawing.Point(208, 0);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(504, 352);
this.listView1.TabIndex = 2;
this.listView1.View = System.Windows.Forms.View.Details;
//
// EmployeeRecordsForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(720, 381);
this.Controls.Add(this.listView1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.treeView1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "EmployeeRecordsForm";
this.Text = "Employee Records Monitoring System";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);

}
#endregion

/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new EmployeeRecordsForm());
}

protected void PopulateTreeView()
{
statusBarPanel1.Text = "Refreshing Employee Codes. Please wait
";
this.Cursor = Cursors.WaitCursor;
treeView1.Nodes.Clear();
tvRootNode = new TreeNode("Employee Records");
this.Cursor = Cursors.Default;
treeView1.Nodes.Add(tvRootNode);
TreeNodeCollection nodeCollect = tvRootNode.Nodes;
string strVal = "";
XmlTextReader reader = new XmlTextReader("E:\\MyFiles\\C#\\Employee Records System\\Employee Records System\\EmpRec.xml");
//reader.MoveToElement();
try
{
while (reader.Read())
{
if (reader.HasAttributes && reader.NodeType == XmlNodeType.Element)
{
//reader.MoveToElement();
reader.MoveToAttribute("Id");
strVal = reader.Value;
reader.Read();
reader.Read();

if (reader.Name == "Dept")
{
reader.Read();
}

// Create the child nodes
TreeNode EcodeNode = new TreeNode(strVal);
// Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text = "Click on an employee code to see their record.";
}
catch (XmlException e)
{
MessageBox.Show("XML Exception :" + e.ToString());
}
}

protected void initializeListControl()
{
listView1.Clear();
listView1.Columns.Add("Employee Name", 255, HorizontalAlignment.Left);
listView1.Columns.Add("Date of Join", 70, HorizontalAlignment.Right);
listView1.Columns.Add("Grade", 105, HorizontalAlignment.Left);
listView1.Columns.Add("Salary", 105, HorizontalAlignment.Left);
}

protected void PopulateListView(TreeNode currNode)
{
initializeListControl();

XmlTextReader listRead = new XmlTextReader("E:\\MyFiles\\C#\\Employee Records System\\Employee Records System\\EmpRec.xml");
//到第一个元素处
listRead.MoveToElement();

while (listRead.Read())
{
string strNodename;
string strNodepath;
string name;
string grade;
string doj;
string sal;
string[] strItemsArr = new string[4];
listRead.MoveToFirstAttribute();
strNodename = listRead.Value;
// currNode.FullPath = "Employee Name\E0001"
strNodepath = currNode.FullPath.Remove(0, 17);
if (strNodepath == strNodename)
{
ListViewItem lvi;
listRead.MoveToNextAttribute();
name = listRead.Value;
lvi = listView1.Items.Add(name);
listRead.Read();
listRead.Read();
listRead.MoveToFirstAttribute();
doj = listRead.Value;
lvi.SubItems.Add(doj);
listRead.MoveToNextAttribute();
grade = listRead.Value;
lvi.SubItems.Add(grade);
listRead.MoveToNextAttribute();
sal = listRead.Value;
lvi.SubItems.Add(sal);
listRead.MoveToNextAttribute();
listRead.MoveToElement();
listRead.ReadString();
}
}
}

private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode currNode = e.Node;
if (tvRootNode == currNode)
{
initializeListControl();
statusBarPanel1.Text = "Double click the Employee Records";
return;
}
else
{
statusBarPanel1.Text = "Click an employee code to view individual records";
}
PopulateListView(currNode);
}
}
}
EmpRec.xml:
<?xml version="1.0" encoding="utf-8" ?>
<EmpRecordsData>
<Ecode Id="E0001" EmployeeName="Michael Perry">
<EmpDetails DateofJoin="02-02-1999" Grade="A" salary="1750"/>
</Ecode>
<Ecode Id="E0002" EmployeeName="Jenifer Carell">
<EmpDetails DateofJoin="03-22-1999" Grade="B" salary="2500"/>
</Ecode>
</EmpRecordsData>
只有一个窗体,EmployeeRecordsForm.cs:
using System;
using System.Drawing;
using System.Collections;
using System.ComponentModel;
using System.Windows.Forms;
using System.Data;
using System.Xml;
using System.IO;
namespace Employee_Records_System
{
/// <summary>
/// Form1 的摘要说明。
/// </summary>
public class EmployeeRecordsForm : System.Windows.Forms.Form
{
private System.Windows.Forms.TreeView treeView1;
private System.Windows.Forms.StatusBar statusBar1;
private System.Windows.Forms.ListView listView1;
private System.Windows.Forms.StatusBarPanel statusBarPanel1;
private TreeNode tvRootNode;
/// <summary>
/// 必需的设计器变量。
/// </summary>
private System.ComponentModel.Container components = null;
public EmployeeRecordsForm()
{
//
// Windows 窗体设计器支持所必需的
//
InitializeComponent();
PopulateTreeView();
initializeListControl();
//
// TODO: 在 InitializeComponent 调用后添加任何构造函数代码
//
}
/// <summary>
/// 清理所有正在使用的资源。
/// </summary>
protected override void Dispose( bool disposing )
{
if( disposing )
{
if (components != null)
{
components.Dispose();
}
}
base.Dispose( disposing );
}
#region Windows 窗体设计器生成的代码
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.treeView1 = new System.Windows.Forms.TreeView();
this.statusBar1 = new System.Windows.Forms.StatusBar();
this.statusBarPanel1 = new System.Windows.Forms.StatusBarPanel();
this.listView1 = new System.Windows.Forms.ListView();
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).BeginInit();
this.SuspendLayout();
//
// treeView1
//
this.treeView1.ImageIndex = -1;
this.treeView1.Location = new System.Drawing.Point(0, 0);
this.treeView1.Name = "treeView1";
this.treeView1.SelectedImageIndex = -1;
this.treeView1.Size = new System.Drawing.Size(208, 352);
this.treeView1.TabIndex = 0;
this.treeView1.AfterSelect += new System.Windows.Forms.TreeViewEventHandler(this.treeView1_AfterSelect);
//
// statusBar1
//
this.statusBar1.Location = new System.Drawing.Point(0, 359);
this.statusBar1.Name = "statusBar1";
this.statusBar1.Panels.AddRange(new System.Windows.Forms.StatusBarPanel[] {
this.statusBarPanel1});
this.statusBar1.ShowPanels = true;
this.statusBar1.Size = new System.Drawing.Size(720, 22);
this.statusBar1.TabIndex = 1;
this.statusBar1.Text = "statusBar1";
//
// statusBarPanel1
//
this.statusBarPanel1.Text = "Click the employee code to view detials";
this.statusBarPanel1.Width = 400;
//
// listView1
//
this.listView1.Activation = System.Windows.Forms.ItemActivation.TwoClick;
this.listView1.Location = new System.Drawing.Point(208, 0);
this.listView1.MultiSelect = false;
this.listView1.Name = "listView1";
this.listView1.Size = new System.Drawing.Size(504, 352);
this.listView1.TabIndex = 2;
this.listView1.View = System.Windows.Forms.View.Details;
//
// EmployeeRecordsForm
//
this.AutoScaleBaseSize = new System.Drawing.Size(6, 14);
this.AutoScroll = true;
this.ClientSize = new System.Drawing.Size(720, 381);
this.Controls.Add(this.listView1);
this.Controls.Add(this.statusBar1);
this.Controls.Add(this.treeView1);
this.MaximizeBox = false;
this.MinimizeBox = false;
this.Name = "EmployeeRecordsForm";
this.Text = "Employee Records Monitoring System";
((System.ComponentModel.ISupportInitialize)(this.statusBarPanel1)).EndInit();
this.ResumeLayout(false);
}
#endregion
/// <summary>
/// 应用程序的主入口点。
/// </summary>
[STAThread]
static void Main()
{
Application.Run(new EmployeeRecordsForm());
}
protected void PopulateTreeView()
{
statusBarPanel1.Text = "Refreshing Employee Codes. Please wait
";
this.Cursor = Cursors.WaitCursor;
treeView1.Nodes.Clear();
tvRootNode = new TreeNode("Employee Records");
this.Cursor = Cursors.Default;
treeView1.Nodes.Add(tvRootNode);
TreeNodeCollection nodeCollect = tvRootNode.Nodes;
string strVal = "";
XmlTextReader reader = new XmlTextReader("E:\\MyFiles\\C#\\Employee Records System\\Employee Records System\\EmpRec.xml");
//reader.MoveToElement();
try
{
while (reader.Read())
{
if (reader.HasAttributes && reader.NodeType == XmlNodeType.Element)
{
//reader.MoveToElement();
reader.MoveToAttribute("Id");
strVal = reader.Value;
reader.Read();
reader.Read();
if (reader.Name == "Dept")
{
reader.Read();
}
// Create the child nodes
TreeNode EcodeNode = new TreeNode(strVal);
// Add the Node
nodeCollect.Add(EcodeNode);
}
}
statusBarPanel1.Text = "Click on an employee code to see their record.";
}
catch (XmlException e)
{
MessageBox.Show("XML Exception :" + e.ToString());
}
}
protected void initializeListControl()
{
listView1.Clear();
listView1.Columns.Add("Employee Name", 255, HorizontalAlignment.Left);
listView1.Columns.Add("Date of Join", 70, HorizontalAlignment.Right);
listView1.Columns.Add("Grade", 105, HorizontalAlignment.Left);
listView1.Columns.Add("Salary", 105, HorizontalAlignment.Left);
}
protected void PopulateListView(TreeNode currNode)
{
initializeListControl();
XmlTextReader listRead = new XmlTextReader("E:\\MyFiles\\C#\\Employee Records System\\Employee Records System\\EmpRec.xml");
//到第一个元素处
listRead.MoveToElement();
while (listRead.Read())
{
string strNodename;
string strNodepath;
string name;
string grade;
string doj;
string sal;
string[] strItemsArr = new string[4];
listRead.MoveToFirstAttribute();
strNodename = listRead.Value;
// currNode.FullPath = "Employee Name\E0001"
strNodepath = currNode.FullPath.Remove(0, 17);
if (strNodepath == strNodename)
{
ListViewItem lvi;
listRead.MoveToNextAttribute();
name = listRead.Value;
lvi = listView1.Items.Add(name);
listRead.Read();
listRead.Read();
listRead.MoveToFirstAttribute();
doj = listRead.Value;
lvi.SubItems.Add(doj);
listRead.MoveToNextAttribute();
grade = listRead.Value;
lvi.SubItems.Add(grade);
listRead.MoveToNextAttribute();
sal = listRead.Value;
lvi.SubItems.Add(sal);
listRead.MoveToNextAttribute();
listRead.MoveToElement();
listRead.ReadString();
}
}
}
private void treeView1_AfterSelect(object sender, TreeViewEventArgs e)
{
TreeNode currNode = e.Node;
if (tvRootNode == currNode)
{
initializeListControl();
statusBarPanel1.Text = "Double click the Employee Records";
return;
}
else
{
statusBarPanel1.Text = "Click an employee code to view individual records";
}
PopulateListView(currNode);
}
}
}EmpRec.xml:
<?xml version="1.0" encoding="utf-8" ?>
<EmpRecordsData>
<Ecode Id="E0001" EmployeeName="Michael Perry">
<EmpDetails DateofJoin="02-02-1999" Grade="A" salary="1750"/>
</Ecode>
<Ecode Id="E0002" EmployeeName="Jenifer Carell">
<EmpDetails DateofJoin="03-22-1999" Grade="B" salary="2500"/>
</Ecode>
</EmpRecordsData>

浙公网安备 33010602011771号