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;
namespace 学生管理系统
{
public partial class Form1 : Form
{
XmlDocument myDoc;
XmlNodeList mylist;
int current = 0;
public Form1()
{
InitializeComponent();
}
private void Form1_Load(object sender, EventArgs e)
{
//程序刚开始运行时显示数据
this.loadxmlNodeList();
this.showStudentInfo(current);
}
//加载xml文件
private void loadxmlNodeList()
{
myDoc = new XmlDocument();
myDoc.Load("students.xml");
mylist = myDoc.GetElementsByTagName("student");
}
//显示学生信息
private void showStudentInfo(int _current)//void 类型的函数返回的值不能是bool类型的
{
//判断下标值出界了就直接返回
if (current < 0 || current > mylist.Count - 1) return;
this.snoBox.Text = mylist[current].Attributes[0].Value;
this.nameBox.Text = mylist[current].ChildNodes[0].InnerText;
this.yearBox.Text = mylist[current].ChildNodes[1].ChildNodes[0].InnerText;
this.monthBox.Text = mylist[current].ChildNodes[1].ChildNodes[1].InnerText;
this.dayBox.Text = mylist[current].ChildNodes[1].ChildNodes[2].InnerText;
if (current == 0)
this.lastOneBtn.Enabled = false;
if (current == mylist.Count - 1)
this.nextOneBtn.Enabled = false;
}
/** 浏览上一个学生信息 */
private void lastOneBtn_Click(object sender, EventArgs e)
{
current--;
this.nextOneBtn.Enabled = true;
this.showStudentInfo(current);
}
/** 浏览上一个学生信息 */
private void nextOneBtn_Click(object sender, EventArgs e)
{
current++;
this.lastOneBtn.Enabled = true;
this.showStudentInfo(current);
}
/** 添加信息按钮 **/
private void addInfoBtn_Click(object sender, EventArgs e)
{
//点击添加信息后把保存按钮和取消按钮显示出来
this.addSaveBtn.Visible = true;
this.addCancleBtn.Visible = true;
//点击按钮之后把窗体上所有的文本框显示内容变为空
this.snoBox.Text =null;//null和“”中间无空格的表示是不一样的,在内存中的存储方式是不一样的
this.nameBox.Text = null;
this.yearBox.Text = null;
this.monthBox.Text = null;
this.dayBox.Text = null;
}
/** 取消按钮 **/
private void addCancleBtn_Click(object sender, EventArgs e)
{
this.addSaveBtn.Visible = false;
this.addCancleBtn.Visible = false;
this.showStudentInfo(current);
}
/** 保存按钮 **/
private void addSaveBtn_Click(object sender, EventArgs e)
{
//创建一个节点,节点名称是student
XmlElement studentnode = myDoc.CreateElement("student");
//创建一个属性,属性名为id
XmlAttribute idnode = myDoc.CreateAttribute("id");
//id属性赋值
idnode.Value = this.snoBox.Text;
//给节点添加属性
studentnode.Attributes.Append(idnode);
XmlElement namenode = myDoc.CreateElement("name");
//给创建的节点赋值
namenode.InnerText = this.nameBox.Text;
studentnode.AppendChild(namenode);
XmlElement birthnode = myDoc.CreateElement("birth");
XmlElement birthyearnode = myDoc.CreateElement("year");
birthyearnode.InnerText = this.yearBox.Text;
birthnode.AppendChild(birthyearnode);
XmlElement birthmontynode = myDoc.CreateElement("month");
birthmontynode.InnerText = this.monthBox.Text;
birthnode.AppendChild(birthmontynode);
XmlElement birthdaynode = myDoc.CreateElement("day");
birthdaynode.InnerText = this.dayBox.Text;
birthnode.AppendChild(birthdaynode);
studentnode.AppendChild(birthnode);
myDoc.ChildNodes[1].AppendChild(studentnode);
myDoc.Save("students.xml");
//防止一直按保存按钮存数多个数据
this.addSaveBtn.Visible = false;
this.addCancleBtn.Visible = false;
}
////封装保存按钮
//private void saveInfo(string _nodeName,string _nodeText)
//{
// XmlElement childNode = myDoc.CreateElement(_nodeName);
// childNode.InnerText = _nodeText;
// studentnode.Attributes.Append(idnode);
//}
}
}