XML读写
private string fileName = HttpContext.Current.Server.MapPath("~/Student.xml");
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
GetAllStudent();
}
}
private void GetAllStudent()
{
ddlStudent.Items.Clear();
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNodeList nodes = doc.GetElementsByTagName("Name");
foreach (XmlElement xml in nodes)
{
ddlStudent.Items.Add(xml.InnerText);
}
}
protected void btnName_Click(object sender, EventArgs e)
{
SelStudent();
}
private void SelStudent()
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
string name = ddlStudent.Text;
XmlNode node = doc.SelectSingleNode("//Student[Name='" + name + "']");
if (node != null)
{
this.txtNo.Text = node.Attributes["ID"].Value;
this.txtName.Text = node.ChildNodes[0].InnerText;
this.txtAge.Text = node.ChildNodes[2].InnerText;
this.txtSex.Text = node.ChildNodes[1].InnerText;
}
}
protected void btnAdd_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode node = doc.DocumentElement;
XmlElement stu = doc.CreateElement("Student");
XmlAttribute stuId = doc.CreateAttribute("ID");
stuId.InnerText = txtNo.Text;
stu.Attributes.Append(stuId);
XmlElement stuName = doc.CreateElement("Name");
stuName.InnerText = txtName.Text;
stu.AppendChild(stuName);
XmlElement stuAge = doc.CreateElement("Age");
stuAge.InnerText = txtAge.Text;
stu.AppendChild(stuAge);
XmlElement stuSex = doc.CreateElement("Sex");
stuSex.InnerText = txtSex.Text;
stu.AppendChild(stuSex);
node.AppendChild(stu);
ScriptManager.RegisterStartupScript(btnAdd, this.GetType(), "del", "alert('添加成功')", true);
doc.Save(fileName);
GetAllStudent();
}
protected void btnUp_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
string name = ddlStudent.Text;
XmlNode stu = doc.SelectSingleNode("//Student[Name='" + name + "']");
if (stu != null)
{
stu.Attributes["ID"].InnerText = txtNo.Text;
stu.ChildNodes[0].InnerText = txtName.Text;
stu.ChildNodes[1].InnerText = txtAge.Text;
stu.ChildNodes[2].InnerText = txtSex.Text;
ScriptManager.RegisterStartupScript(btnUp, this.GetType(), "del", "alert('更新成功')", true);
}
doc.Save(fileName);
GetAllStudent();
}
protected void btnDel_Click(object sender, EventArgs e)
{
XmlDocument doc = new XmlDocument();
doc.Load(fileName);
XmlNode node = doc.DocumentElement;
string name = ddlStudent.Text;
XmlNode stu = doc.SelectSingleNode("//Student[Name='" + name + "']");
if (stu != null)
{
node.RemoveChild(stu);
ScriptManager.RegisterStartupScript(btnDel, this.GetType(), "del", "alert('删除成功')", true);
}
doc.Save(fileName);
GetAllStudent();
}
protected void btnAllStudent_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(fileName);
lblTable.Text = ds.Tables[0].TableName;
this.grdStudent.DataSource = ds.Tables[0];
this.grdStudent.DataBind();
}
protected void btnInsertRow_Click(object sender, EventArgs e)
{
DataSet ds = new DataSet();
ds.ReadXml(fileName);
DataTable dt = ds.Tables[0];
DataRow rs = dt.NewRow();
rs["ID"] = this.txtNo.Text;
rs["Name"] = this.txtName.Text;
rs["Age"] = this.txtAge.Text;
rs["Sex"] = this.txtSex.Text;
dt.Rows.Add(rs);
ds.WriteXml(fileName);
ScriptManager.RegisterStartupScript(btnInsertRow, GetType(), "insert", "alert('插入成功')", true);
GetAllStudent();
}