xml_操作
using System;
using System.Collections;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Web;
using System.Web.SessionState;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.UI.HtmlControls;
using System.IO;
using System.Xml;
namespace XmlDom
{
/// <summary>
/// WebForm1 的摘要说明。
/// </summary>
public class WebForm1 : System.Web.UI.Page
{
protected System.Web.UI.WebControls.TextBox TextBox1;
protected System.Web.UI.WebControls.TextBox TextBox2;
protected System.Web.UI.WebControls.TextBox TextBox3;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.DataGrid DataGrid1;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
ReadXml();
}
private void ReadXml()
{
string url = Server.MapPath("User.xml");//获得当前文件夹下的XML文件
StreamReader sRead = new StreamReader(url,System.Text.Encoding.GetEncoding("GB2312"));//以一种特定的编码从字节流读取字符,必须要转化成GB2312读取才不能出乱码
XmlDataDocument datadoc = new XmlDataDocument();//操作XML文档
datadoc.DataSet.ReadXml(sRead);//将读取的字节流存到DataSet里面去
this.DataGrid1.DataSource = datadoc.DataSet.Tables[0].DefaultView;
DataGrid1.DataKeyField="username";//以username建立索引
this.DataGrid1.DataBind();
datadoc = null;//清空对XML数据的操作
sRead.Close();//关闭字节流的读取
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.DataGrid1.CancelCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_CancelCommand);
this.DataGrid1.EditCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_EditCommand);
this.DataGrid1.UpdateCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_UpdateCommand);
this.DataGrid1.DeleteCommand += new System.Web.UI.WebControls.DataGridCommandEventHandler(this.DataGrid1_DeleteCommand);
this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void DataGrid1_DeleteCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string url = Server.MapPath("user.xml");
string str_username=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(url); //加载XML文件
XmlNode root=xmlDoc.SelectSingleNode("UserInfo");//查找UserInfo节点
XmlNodeList xnl=root.ChildNodes; //获取UserInfo下面所以子节点
for(int i=0;i<xnl.Count;i++)
{
XmlNodeList xnl11=xnl.Item(i).SelectNodes("username");//查找username下面节点集合
XmlElement xe=(XmlElement)xnl11.Item(0); //username节点下面的元素
if(xe.InnerText==str_username) //获取节点以及所有子集的串联值
{
root.RemoveChild(xe.ParentNode);//移除指顶节点
// xe.ParentNode.RemoveChild(xe);
// xnl.Item(i).RemoveChild(xe);
// if(i<xnl.Count)i=i-1;
}
}
xmlDoc.Save(url);
ReadXml();
}
private void Button1_Click(object sender, System.EventArgs e)
{
string url = Server.MapPath("user.xml");
string str_username = this.TextBox1.Text.ToString();
string str_password = this.TextBox2.Text.ToString();
string str_study = this.TextBox3.Text.ToString();
try
{
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(url); //加载XML文档
XmlNode root=xmlDoc.SelectSingleNode("UserInfo");//查找第一个匹配的XmlNode
XmlElement xe1=xmlDoc.CreateElement("datarow");//创建第一个节点的第一个元素
XmlElement xesub1=xmlDoc.CreateElement("username"); //创建第一个节点的第一个元素里的属性
xesub1.InnerText=str_username;//设置文本节点
xe1.AppendChild(xesub1);//将指定节点添加到该节点的子节点列表的末尾
XmlElement xesub2=xmlDoc.CreateElement("password");
xesub2.InnerText=str_password;
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("study");
xesub3.InnerText=str_study;
xe1.AppendChild(xesub3);
root.AppendChild(xe1);
xmlDoc.Save (url);//保存XML文件
ReadXml();
}
catch (Exception ex)
{
Response.Write(ex.ToString());
}
}
private void DataGrid1_EditCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=e.Item.ItemIndex;
ReadXml();
}
private void DataGrid1_CancelCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
this.DataGrid1.EditItemIndex=-1;
ReadXml();
}
private void DataGrid1_UpdateCommand(object source, System.Web.UI.WebControls.DataGridCommandEventArgs e)
{
string url = Server.MapPath("user.xml");
string str_username=DataGrid1.DataKeys[e.Item.ItemIndex].ToString();
TextBox txt_username = (TextBox)(e.Item.Cells[2].Controls[0]);
TextBox txt_password = (TextBox)(e.Item.Cells[3].Controls[0]);
TextBox drp_study=(TextBox)(e.Item.Cells[4].Controls[0]);
XmlDocument xmlDoc=new XmlDocument();
xmlDoc.Load(url); //加载XML文件
XmlNode root=xmlDoc.SelectSingleNode("UserInfo");//查找UserInfo节点
XmlNodeList xnl=root.ChildNodes; //获取UserInfo下面所以子节点
for(int i=0;i<xnl.Count;i++)
{
XmlNodeList xnl11=xnl.Item(i).SelectNodes("username");//查找username下面节点集合
XmlElement xe=(XmlElement)xnl11.Item(0); //username节点下面的元素
if(xe.InnerText==str_username) //获取节点以及所有子集的串联值
{
xe.InnerText=txt_username.Text;
xe.ParentNode.ChildNodes.Item(1).InnerText=txt_password.Text;
xe.ParentNode.ChildNodes.Item(2).InnerText=drp_study.Text;
}
}
xmlDoc.Save(url);
this.DataGrid1.EditItemIndex = -1;
ReadXml();
}
}
}