c#来修改XML文档中某个接点的值 20
2个回答
展开全部
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 DataGridTest
{
/// <summary>
/// XML 的摘要说明。
/// </summary>
public class XML : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btadd;
protected System.Web.UI.WebControls.Button btedit;
protected System.Web.UI.WebControls.Button btdelete;
protected System.Web.UI.WebControls.Button btselect;
private XmlDocument xmldoc;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btselect.Click += new System.EventHandler(this.btselect_Click);
this.btadd.Click += new System.EventHandler(this.btadd_Click);
this.btedit.Click += new System.EventHandler(this.btedit_Click);
this.btdelete.Click += new System.EventHandler(this.btdelete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void loadxml()
{
xmldoc=new XmlDocument ();
xmldoc.Load (Server.MapPath (".\\cd.xml"));//加载XML
}
private void btselect_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode docment=xmldoc.DocumentElement ;
XmlNodeList nodelist=docment.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="3")
{
XmlNode child=x.SelectSingleNode ("xiao");
XmlNode select=child.SelectSingleNode ("地址");
string address=select.InnerText;//获取所要查询节点的串联值
this.Response .Write (address);
}
}
}
private void btadd_Click(object sender, System.EventArgs e)
{
loadxml();
//XmlNode node=xmldoc.DocumentElement ;//获取根节点
XmlNode document=xmldoc.DocumentElement ;
/*XmlNodeList nodelist=document.ChildNodes ;
XmlNode node=null;
foreach(XmlNode i in nodelist)
{
XmlElement xl=(XmlElement)i;
if(xl.GetAttribute ("name")=="3")
node=i;
}
*///这边用于查找根节点的子节点CD中,属性name等于3的节点,下面的信息将插入到指定的节点下;
XmlNode node=document.SelectSingleNode ("CD");//查找节点名为CD的节点
XmlElement element=xmldoc.CreateElement("xiao");//创建新的元素
element.SetAttribute ("姓","陈");//给新元素添加属性
element.SetAttribute ("性别","男");
element.SetAttribute ("年龄","22");
XmlElement el1=xmldoc.CreateElement ("地址");
el1.InnerText ="福建省";
element.AppendChild (el1);//将指定的节点添加到该节点的末尾
XmlElement el2=xmldoc.CreateElement ("毕业院校");
el2.InnerText ="褔州大学工程技术学院";
element.AppendChild (el2);
node.AppendChild (element);
xmldoc.Save (Server.MapPath (".\\cd.xml"));//将XML文档保存到指定的文件下
}
private void btedit_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode node=xmldoc.DocumentElement ;
XmlNodeList nodelist=node.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="3")// 获得对应属性值(GetAttribute获取属性)
{
XmlElement child=(XmlElement)xl.SelectSingleNode ("xiao");//取得名为xiao的节点,并转换为XmlElement元素类型
if(child.GetAttribute ("年龄")=="22")
{
child.SetAttribute ("年龄","21");//重新设定属性值
}
}
}
xmldoc.Save (Server.MapPath (".\\cd.xml"));
}
private void btdelete_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode document=xmldoc.DocumentElement ;
XmlNodeList nodelist=document.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="1")
{
XmlNode xiao=x.SelectSingleNode ("xiao");
XmlNode address=xiao.SelectSingleNode ("毕业院校");
//address.RemoveAll();//删除该节点的所有子节点。。。不包括该节点
xiao.RemoveChild (address);//删除指定的子节点。。。参数为节点类型(XmlNode);
xmldoc.Save (Server.MapPath (".\\cd.xml"));
}
}
}
}
}
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 DataGridTest
{
/// <summary>
/// XML 的摘要说明。
/// </summary>
public class XML : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Button btadd;
protected System.Web.UI.WebControls.Button btedit;
protected System.Web.UI.WebControls.Button btdelete;
protected System.Web.UI.WebControls.Button btselect;
private XmlDocument xmldoc;
private void Page_Load(object sender, System.EventArgs e)
{
// 在此处放置用户代码以初始化页面
}
#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}
/// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>
private void InitializeComponent()
{
this.btselect.Click += new System.EventHandler(this.btselect_Click);
this.btadd.Click += new System.EventHandler(this.btadd_Click);
this.btedit.Click += new System.EventHandler(this.btedit_Click);
this.btdelete.Click += new System.EventHandler(this.btdelete_Click);
this.Load += new System.EventHandler(this.Page_Load);
}
#endregion
private void loadxml()
{
xmldoc=new XmlDocument ();
xmldoc.Load (Server.MapPath (".\\cd.xml"));//加载XML
}
private void btselect_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode docment=xmldoc.DocumentElement ;
XmlNodeList nodelist=docment.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="3")
{
XmlNode child=x.SelectSingleNode ("xiao");
XmlNode select=child.SelectSingleNode ("地址");
string address=select.InnerText;//获取所要查询节点的串联值
this.Response .Write (address);
}
}
}
private void btadd_Click(object sender, System.EventArgs e)
{
loadxml();
//XmlNode node=xmldoc.DocumentElement ;//获取根节点
XmlNode document=xmldoc.DocumentElement ;
/*XmlNodeList nodelist=document.ChildNodes ;
XmlNode node=null;
foreach(XmlNode i in nodelist)
{
XmlElement xl=(XmlElement)i;
if(xl.GetAttribute ("name")=="3")
node=i;
}
*///这边用于查找根节点的子节点CD中,属性name等于3的节点,下面的信息将插入到指定的节点下;
XmlNode node=document.SelectSingleNode ("CD");//查找节点名为CD的节点
XmlElement element=xmldoc.CreateElement("xiao");//创建新的元素
element.SetAttribute ("姓","陈");//给新元素添加属性
element.SetAttribute ("性别","男");
element.SetAttribute ("年龄","22");
XmlElement el1=xmldoc.CreateElement ("地址");
el1.InnerText ="福建省";
element.AppendChild (el1);//将指定的节点添加到该节点的末尾
XmlElement el2=xmldoc.CreateElement ("毕业院校");
el2.InnerText ="褔州大学工程技术学院";
element.AppendChild (el2);
node.AppendChild (element);
xmldoc.Save (Server.MapPath (".\\cd.xml"));//将XML文档保存到指定的文件下
}
private void btedit_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode node=xmldoc.DocumentElement ;
XmlNodeList nodelist=node.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="3")// 获得对应属性值(GetAttribute获取属性)
{
XmlElement child=(XmlElement)xl.SelectSingleNode ("xiao");//取得名为xiao的节点,并转换为XmlElement元素类型
if(child.GetAttribute ("年龄")=="22")
{
child.SetAttribute ("年龄","21");//重新设定属性值
}
}
}
xmldoc.Save (Server.MapPath (".\\cd.xml"));
}
private void btdelete_Click(object sender, System.EventArgs e)
{
loadxml();
XmlNode document=xmldoc.DocumentElement ;
XmlNodeList nodelist=document.ChildNodes ;
foreach(XmlNode x in nodelist)
{
XmlElement xl=(XmlElement)x;
if(xl.GetAttribute ("name")=="1")
{
XmlNode xiao=x.SelectSingleNode ("xiao");
XmlNode address=xiao.SelectSingleNode ("毕业院校");
//address.RemoveAll();//删除该节点的所有子节点。。。不包括该节点
xiao.RemoveChild (address);//删除指定的子节点。。。参数为节点类型(XmlNode);
xmldoc.Save (Server.MapPath (".\\cd.xml"));
}
}
}
}
}
已赞过
已踩过<
评论
收起
你对这个回答的评价是?
推荐律师服务:
若未解决您的问题,请您详细描述您的问题,通过百度律临进行免费专业咨询