asp.net中对XML文件进行操作(增、删、改、查)
2007-01-29 14:08
405 查看
现在随着网络发展,xml的应用也被不断应用到网络中.本篇主要是针对对Xml的应用进行一些实际操作.代码稚嫩.供自学..HOHO~~
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.Xml;
namespace DVBAres_C2._2
...{
/**//// <summary>
/// Information_page_templates 的摘要说明。
/// </summary>
public class Information_page_templates : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.TextBox tbNewTemplatesName;
protected System.Web.UI.WebControls.Button btnChange;
protected System.Web.UI.WebControls.TextBox TextBoxTemplatesName;
protected System.Web.UI.WebControls.Button btnQuery;
protected System.Web.UI.WebControls.DropDownList ddlName;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Label dgTitle;
protected System.Web.UI.WebControls.Button Button_NewTemplates;
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
try
...{
if(!IsPostBack)
...{
Bind();
}
}
catch
...{
Response.Write("没有发现模板库中有任何模板!!!");
}
btnDelete.Attributes.Add("onclick","return confirm('你确定要删除该模板吗?')");
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Button_NewTemplates.Click += new System.EventHandler(this.Button_NewTemplates_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Bind()
...{
//XML数据绑定
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("TemplatesName");
ddlName.Items.Clear();
for(int i=0;i<elemList.Count;i++)
ddlName.Items.Add(elemList[i].InnerXml);
//XmlTemplates=ddlName.SelectedItem.Value;
}
private void Button_NewTemplates_Click(object sender, System.EventArgs e)
...{
string fileName = Server.MapPath(@".TemplatesStatistical_charts_types_templates.xml"); //要打开的文件
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNode root=xmlDoc.SelectSingleNode("DVBAres");//查找<Dvb>
XmlNodeList xnl=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;
int nIndex = xnl.Count;
//以下添加新结点
XmlElement xe1=xmlDoc.CreateElement("Templates");//创建一个Templates节点
XmlElement xesub1=xmlDoc.CreateElement("TemplatesID");
xesub1.InnerText=nIndex.ToString();//设置文本节点
xe1.AppendChild(xesub1);//添加到Templates节点中
XmlElement xesub2=xmlDoc.CreateElement("TemplatesName");
xesub2.InnerText=TextBox_TemplatesName.Text;
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("TemplatesChan");
xesub3.InnerText=Chan_List.SelectedItem.Value;//获取下拉列表索引值
//xesub3.InnerText=Chan_List.SelectedItem.Text;//获取下拉列表TEXT
xe1.AppendChild(xesub3);
XmlElement xesub4=xmlDoc.CreateElement("TemplatesErr");
xesub4.InnerText=Err_List.SelectedValuesToString();
//xesub4.InnerText=Err_List.SelectedLabelsToString();
xe1.AppendChild(xesub4);
XmlElement xesub5=xmlDoc.CreateElement("TemplatesStat");
xesub5.InnerText=DropDownListStat.SelectedItem.Value;
//xesub5.InnerText=DropDownListStat.SelectedItem.Text;
xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<Dvb>节点中
xmlDoc.Save(fileName);
Page.Response.Write("生成模版成功!");
}
catch
...{
Page.Response.Write("生成模板失败!请检查路径是否正确,以及是否有写入权限。");
}
}
private void btnChange_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList nodeList=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;//获取dbGuest节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
...{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("TemplatesName");
if(node.Count>0)
...{
if(node[0].InnerText==ddlName.SelectedItem.Text)
...{
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
...{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="TemplatesName")//如果找到
...{
xe2.InnerText=this.tbNewTemplatesName.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
Bind();
Page.Response.Write("修改成功!");
}
catch
...{
Page.Response.Write("你要查看的文件路径不对,请检查路径是否正确,以及是否有写入权限");
}
}
private void btnQuery_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
TextBoxTemplatesName.Text = doc.SelectSingleNode("//Templates[TemplatesName='"+ddlName.SelectedItem.Text+"']").ChildNodes.Item(1).InnerText;
}
catch
...{
Page.Response.Write("你要查看的文件路径不对,请检查路径是否正确");
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList xnl=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;
foreach(XmlNode xn in xnl)
...{
XmlElement xe=(XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("TemplatesName");
if(node.Count>0)
...{
if(node[0].InnerText==ddlName.SelectedItem.Text)
...{
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
}
xmlDoc.Save(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
Bind();
Page.Response.Write("删除模版成功!");
}
catch
...{
Page.Response.Write("删除模板失败!请检查路径是否正确,以及模板库中是否还有模板.以及是否有写入权限。");
}
}
}
}
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.Xml;
namespace DVBAres_C2._2
...{
/**//// <summary>
/// Information_page_templates 的摘要说明。
/// </summary>
public class Information_page_templates : System.Web.UI.Page
...{
protected System.Web.UI.WebControls.TextBox tbNewTemplatesName;
protected System.Web.UI.WebControls.Button btnChange;
protected System.Web.UI.WebControls.TextBox TextBoxTemplatesName;
protected System.Web.UI.WebControls.Button btnQuery;
protected System.Web.UI.WebControls.DropDownList ddlName;
protected System.Web.UI.WebControls.Button btnDelete;
protected System.Web.UI.WebControls.Label dgTitle;
protected System.Web.UI.WebControls.Button Button_NewTemplates;
protected System.Web.UI.WebControls.DataGrid dgShow;
private void Page_Load(object sender, System.EventArgs e)
...{
// 在此处放置用户代码以初始化页面
try
...{
if(!IsPostBack)
...{
Bind();
}
}
catch
...{
Response.Write("没有发现模板库中有任何模板!!!");
}
btnDelete.Attributes.Add("onclick","return confirm('你确定要删除该模板吗?')");
}
Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
...{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
...{
this.btnChange.Click += new System.EventHandler(this.btnChange_Click);
this.btnQuery.Click += new System.EventHandler(this.btnQuery_Click);
this.btnDelete.Click += new System.EventHandler(this.btnDelete_Click);
this.Button_NewTemplates.Click += new System.EventHandler(this.Button_NewTemplates_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void Bind()
...{
//XML数据绑定
DataSet ds = new DataSet();
ds.ReadXml(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
dgShow.DataSource = ds.Tables[0].DefaultView;
dgShow.DataBind();
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList elemList = doc.GetElementsByTagName("TemplatesName");
ddlName.Items.Clear();
for(int i=0;i<elemList.Count;i++)
ddlName.Items.Add(elemList[i].InnerXml);
//XmlTemplates=ddlName.SelectedItem.Value;
}
private void Button_NewTemplates_Click(object sender, System.EventArgs e)
...{
string fileName = Server.MapPath(@".TemplatesStatistical_charts_types_templates.xml"); //要打开的文件
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(fileName);
XmlNode root=xmlDoc.SelectSingleNode("DVBAres");//查找<Dvb>
XmlNodeList xnl=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;
int nIndex = xnl.Count;
//以下添加新结点
XmlElement xe1=xmlDoc.CreateElement("Templates");//创建一个Templates节点
XmlElement xesub1=xmlDoc.CreateElement("TemplatesID");
xesub1.InnerText=nIndex.ToString();//设置文本节点
xe1.AppendChild(xesub1);//添加到Templates节点中
XmlElement xesub2=xmlDoc.CreateElement("TemplatesName");
xesub2.InnerText=TextBox_TemplatesName.Text;
xe1.AppendChild(xesub2);
XmlElement xesub3=xmlDoc.CreateElement("TemplatesChan");
xesub3.InnerText=Chan_List.SelectedItem.Value;//获取下拉列表索引值
//xesub3.InnerText=Chan_List.SelectedItem.Text;//获取下拉列表TEXT
xe1.AppendChild(xesub3);
XmlElement xesub4=xmlDoc.CreateElement("TemplatesErr");
xesub4.InnerText=Err_List.SelectedValuesToString();
//xesub4.InnerText=Err_List.SelectedLabelsToString();
xe1.AppendChild(xesub4);
XmlElement xesub5=xmlDoc.CreateElement("TemplatesStat");
xesub5.InnerText=DropDownListStat.SelectedItem.Value;
//xesub5.InnerText=DropDownListStat.SelectedItem.Text;
xe1.AppendChild(xesub5);
root.AppendChild(xe1);//添加到<Dvb>节点中
xmlDoc.Save(fileName);
Page.Response.Write("生成模版成功!");
}
catch
...{
Page.Response.Write("生成模板失败!请检查路径是否正确,以及是否有写入权限。");
}
}
private void btnChange_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList nodeList=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;//获取dbGuest节点的所有子节点
foreach(XmlNode xn in nodeList)//遍历所有子节点
...{
XmlElement xe=(XmlElement)xn;//将子节点类型转换为XmlElement类型
XmlNodeList node = xe.GetElementsByTagName("TemplatesName");
if(node.Count>0)
...{
if(node[0].InnerText==ddlName.SelectedItem.Text)
...{
XmlNodeList nls=xe.ChildNodes;//继续获取xe子节点的所有子节点
foreach(XmlNode xn1 in nls)//遍历
...{
XmlElement xe2=(XmlElement)xn1;//转换类型
if(xe2.Name=="TemplatesName")//如果找到
...{
xe2.InnerText=this.tbNewTemplatesName.Text;//则修改
break;//找到退出来就可以了
}
}
break;
}
}
}
xmlDoc.Save(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
Bind();
Page.Response.Write("修改成功!");
}
catch
...{
Page.Response.Write("你要查看的文件路径不对,请检查路径是否正确,以及是否有写入权限");
}
}
private void btnQuery_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument doc = new XmlDocument();
doc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
TextBoxTemplatesName.Text = doc.SelectSingleNode("//Templates[TemplatesName='"+ddlName.SelectedItem.Text+"']").ChildNodes.Item(1).InnerText;
}
catch
...{
Page.Response.Write("你要查看的文件路径不对,请检查路径是否正确");
}
}
private void btnDelete_Click(object sender, System.EventArgs e)
...{
try
...{
XmlDocument xmlDoc = new XmlDocument();
xmlDoc.Load(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
XmlNodeList xnl=xmlDoc.SelectSingleNode("DVBAres").ChildNodes;
foreach(XmlNode xn in xnl)
...{
XmlElement xe=(XmlElement)xn;
XmlNodeList node = xe.GetElementsByTagName("TemplatesName");
if(node.Count>0)
...{
if(node[0].InnerText==ddlName.SelectedItem.Text)
...{
xe.RemoveAll();//删除该节点的全部内容
break;
}
}
}
xmlDoc.Save(Server.MapPath("./Templates/Statistical_charts_types_templates.xml"));
Bind();
Page.Response.Write("删除模版成功!");
}
catch
...{
Page.Response.Write("删除模板失败!请检查路径是否正确,以及模板库中是否还有模板.以及是否有写入权限。");
}
}
}
}
相关文章推荐
- ASP.NET中对SQLITE数据库进行插入操作后返回自增的ID
- asp.net 对xml文件的读写,添加,修改,删除操作
- asp.net 对xml文件的读写,添加,修改,删除操作
- asp.net——网站发布后xml文件拒绝写入操作
- ASP.net(C#)下操作XML文件
- Asp.Net对Xml文件的操作
- asp.net操作xml文件的代码实例
- ASP.NET对IIS中的虚拟目录进行操作
- Asp.Net 操作XML文件的增删改查 利用GridView
- 在ASP.NET 2.0中操作数据之六十一:在事务里对数据库修改进行封装
- 在asp.net中对XML文档进行的操作(不全,待续)
- asp.net 对xml文件的读写,添加,修改,删除操作
- [导入]Silverlight 2 (beta1)数据操作(2)——使用ASP.NET Web Service进行数据CRUD操作(下)
- ASP.NET MVC 标签绑定传值及后台获取并进行修改操作
- ASP.Net 对xml文件的读写,添加,修改,删除操作
- asp.net Aspose.Word 利用邮件合并功能 操作word模板 生成word。asp.net NPOI 对excel进行操作,生成excel
- asp.net 对xml文件的读写,添加,修改,删除操作
- ASP.NET中对SQLITE数据库进行插入操作后返回自增的ID
- 在ASP.NET 2.0中操作数据::基于用户对修改数据进行限制