读写xml(C#例子,asp.net)

aspx页面
<% @ Page language = " c# "  Codebehind = " xdoc.aspx.cs "  AutoEventWireup = " false "  Inherits = " XML.xdoc "   %>
<! DOCTYPE HTML PUBLIC  " -//W3C//DTD HTML 4.0 Transitional//EN "   >
< HTML >
< HEAD >
< title > xdoc </ title >
< meta content = " Microsoft Visual Studio .NET 7.1 "  name = " GENERATOR " >
< meta content = " C# "  name = " CODE_LANGUAGE " >
< meta content = " JavaScript "  name = " vs_defaultClientScript " >
< meta content = " http://schemas.microsoft.com/intellisense/ie5 "  name = " vs_targetSchema " >
</ HEAD >
< body MS_POSITIONING = " GridLayout " >
< form id = " Form1 "  method = " post "  runat = " server " >
< FONT face = " 宋体 " >
< asp:datagrid id = " dg "  style = " Z-INDEX: 101; LEFT: 48px; POSITION: absolute; TOP: 

56px
"  runat= " server "
ForeColor = " Black "  BorderStyle = " None "  BorderWidth = " 1px "  BorderColor = " #DEDFDE "  

BackColor
= " White "
CellPadding
= " 4 "  GridLines = " Vertical "  AutoGenerateColumns = " False "  

Width
= " 432px " >
< SelectedItemStyle Font - Bold = " True "  ForeColor = " White "  

BackColor
= " #CE5D5A " ></ SelectedItemStyle >
< AlternatingItemStyle BackColor = " White " ></ AlternatingItemStyle >
< ItemStyle BackColor = " #F7F7DE " ></ ItemStyle >
< HeaderStyle Font - Bold = " True "  ForeColor = " White "  

BackColor
= " #6B696B " ></ HeaderStyle >
< FooterStyle BackColor = " #CCCC99 " ></ FooterStyle >
< Columns >
< asp:BoundColumn DataField = " name "  HeaderText = " 名字 " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " City "  HeaderText = " 城市 " ></ asp:BoundColumn >
< asp:BoundColumn DataField = " Email "  HeaderText = " 邮件地

" ></asp:BoundColumn>
< asp:BoundColumn DataField = " Time "  HeaderText = " 时间 " ></ asp:BoundColumn >
</ Columns >
< PagerStyle HorizontalAlign = " Right "  ForeColor = " Black "  BackColor = " #F7F7DE "  

Mode
= " NumericPages " ></ PagerStyle >
</ asp:datagrid >< asp:label id = " Label1 "  style = " Z-INDEX: 102; LEFT: 112px; POSITION: 

absolute; TOP: 8px
"  runat= " server "
Width = " 336px "  BorderWidth = " 2px "  BorderStyle = " Ridge "  Font - Size = " X-Large "  Font

- Bold = " True "  ForeColor = " Red " > XmlDocument类的挖掘 </ asp:label >< asp:dropdownlist id = " ddl "  style = " Z-

INDEX: 
103 ; LEFT: 128px; POSITION: absolute; TOP: 280px "  

runat
= " server " ></ asp:dropdownlist >< asp:label id = " Label2 "  style = " Z-INDEX: 104; LEFT: 64px; 

POSITION: absolute; TOP: 280px
"  runat= " server " >姓名:</asp:label><asp:button id= " Button1 "  

style
= " Z-INDEX: 105; LEFT: 64px; POSITION: absolute; TOP: 328px "  runat = " server "
Width
= " 64px "  Text = " 查询 " ></ asp:button >< asp:label id = " Label3 "  style = " Z-INDEX: 

106 ; LEFT: 64px; POSITION: absolute; TOP: 384px "  runat= " server " >邮件地址:</asp:label><asp:button 

id
= " Button2 "  style = " Z-INDEX: 107; LEFT: 152px; POSITION: absolute; TOP: 328px "  runat = " server "
Width
= " 64px "  Text = " 删除 " ></ asp:button >< asp:button id = " Button3 "  style = " Z-

INDEX: 
108 ; LEFT: 432px; POSITION: absolute; TOP: 280px "  runat= " server "
Width = " 56px "  Text = " 添加 "  Height = " 80px " ></ asp:button >< asp:textbox id = " tbn "  

style
= " Z-INDEX: 109; LEFT: 152px; POSITION: absolute; TOP: 384px "  runat = " server "
Width
= " 328px " ></ asp:textbox >< asp:button id = " Button4 "  style = " Z-INDEX: 110; 

LEFT: 64px; POSITION: absolute; TOP: 416px
"  runat= " server "
Width = " 424px "  Text = " 修改邮件地址 " ></ asp:button >
< asp:TextBox id = " tbna "  style = " Z-INDEX: 111; LEFT: 312px; POSITION: absolute; TOP: 

280px
"  runat= " server "
Width = " 104px " ></ asp:TextBox >
< asp:TextBox id = " tbc "  style = " Z-INDEX: 112; LEFT: 312px; POSITION: absolute; TOP: 

312px
"  runat= " server "
Width = " 104px "  Height = " 24px " ></ asp:TextBox >
< asp:TextBox id = " tbe "  style = " Z-INDEX: 113; LEFT: 312px; POSITION: absolute; TOP: 

344px
"  runat= " server "
Width = " 104px "  Height = " 24px " ></ asp:TextBox >
< asp:Label id = " Label4 "  style = " Z-INDEX: 114; LEFT: 264px; POSITION: absolute; TOP: 

280px
"  runat= " server " >姓名:</asp:Label>
< asp:Label id = " Label5 "  style = " Z-INDEX: 115; LEFT: 264px; POSITION: absolute; TOP: 

312px
"  runat= " server " >城市:</asp:Label>
< asp:Label id = " Label6 "  style = " Z-INDEX: 116; LEFT: 264px; POSITION: absolute; TOP: 

344px
"  runat= " server " >Email:</asp:Label></FONT></form>
</ body >
</ HTML >
 cs文件
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;
using  System.Xml.XPath;

namespace  XML
{
/**//// <summary>
/// xdoc 的摘要说明。
/// </summary>

public class xdoc : System.Web.UI.Page
{
protected System.Web.UI.WebControls.Label Label1;
protected System.Web.UI.WebControls.Label Label2;
protected System.Web.UI.WebControls.Button Button1;
protected System.Web.UI.WebControls.Label Label3;
protected System.Web.UI.WebControls.Button Button2;
protected System.Web.UI.WebControls.Button Button3;
protected System.Web.UI.WebControls.DataGrid dg;
protected System.Web.UI.WebControls.DropDownList ddl;
protected System.Web.UI.WebControls.TextBox tbn;
protected System.Web.UI.WebControls.Label Label4;
protected System.Web.UI.WebControls.Label Label5;
protected System.Web.UI.WebControls.Label Label6;
protected System.Web.UI.WebControls.TextBox tbna;
protected System.Web.UI.WebControls.TextBox tbc;
protected System.Web.UI.WebControls.TextBox tbe;
protected System.Web.UI.WebControls.Button Button4;

private void Page_Load(object sender, System.EventArgs e)
{//看动作名字就知道了是载入了
if(!Page.IsPostBack)
{
fill();
}

}


//Web 窗体设计器生成的代码#region Web 窗体设计器生成的代码
override protected void OnInit(EventArgs e)
{
//
// CODEGEN: 该调用是 ASP.NET Web 窗体设计器所必需的。
//
InitializeComponent();
base.OnInit(e);
}


/**//// <summary>
/// 设计器支持所需的方法 - 不要使用代码编辑器修改
/// 此方法的内容。
/// </summary>

private void InitializeComponent()

this.Button1.Click += new System.EventHandler(this.Button1_Click);
this.Button2.Click += new System.EventHandler(this.Button2_Click);
this.Button3.Click += new System.EventHandler(this.Button3_Click);
this.Button4.Click += new System.EventHandler(this.Button4_Click);
this.Load += new System.EventHandler(this.Page_Load);

}


private void fill()
{//绑定方法
DataSet ds=new DataSet();
ds.ReadXml(Server.MapPath(
"/xml/xml/doc.xml"));
dg.DataSource
=ds.Tables[0];
dg.DataBind();
XmlDocument xd
=new XmlDocument();
xd.Load(Server.MapPath(
"/xml/xml/doc.xml"));
System.Xml.XmlNodeList xnl
=xd.GetElementsByTagName("Name");
ddl.Items.Clear();
for(int i=0;i<xnl.Count;i++)
{
ddl.Items.Add(xnl[i].InnerText);
}

}

private void Button1_Click(object sender, System.EventArgs e)
{//查询动作
XmlDocument xd=new XmlDocument();
xd.Load(Server.MapPath(
"/xml/xml/doc.xml"));
tbn.Text
=xd.SelectSingleNode("//User

[Name
='"+ddl.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText.ToString();
}

private void Button2_Click(object sender, System.EventArgs e)
{//删除动作
XmlDocument xdoc=new XmlDocument();
xdoc.Load(Server.MapPath(
"/xml/xml/doc.xml"));
XmlNodeList xnl
=xdoc.SelectSingleNode("dbGuest").ChildNodes;

foreach(XmlNode xn in xnl)
{
XmlElement xe
=(XmlElement)xn;
XmlNodeList node
=xe.GetElementsByTagName("Name");//查找他的Name行
if(node.Count>0)
{
if(node[0].InnerText==ddl.SelectedItem.Text)//如果当前节点的名字和下拉列表传

来的一样,就删除
{
xe.RemoveAll();
//111
}

break;
}


}

xdoc.Save(Server.MapPath(
"/xml/xml/doc.xml"));
fill();
}


private void Button3_Click(object sender, System.EventArgs e)
{//添加动作
string name=this.tbna.Text.ToString();
string city=this.tbc.Text.ToString();
string email=this.tbe.Text.ToString();

XmlDocument xd
=new XmlDocument();
xd.Load(Server.MapPath(
"/xml/xml/doc.xml"));
//查找dbGuest节点
XmlNode xn=xd.SelectSingleNode("dbGuest");
//创建user节点
XmlElement xe_u=xd.CreateElement("User");
//创建一个name元素
XmlElement xe_a1=xd.CreateElement("Name");
xe_a1.InnerText
=name;//设置该元素的值
xe_u.AppendChild(xe_a1);//把刚才创建的元素插入name节点里面去
XmlElement xe_a2=xd.CreateElement("City");
xe_a2.InnerText
=city;
xe_u.AppendChild(xe_a2);
XmlElement xe_a3
=xd.CreateElement("Email");
xe_a3.InnerText
=email;
xe_u.AppendChild(xe_a3);
XmlElement xe_a4
=xd.CreateElement("Time");
xe_a4.InnerText
=(DateTime.Now).ToString();
xe_u.AppendChild(xe_a4);
//将User节点插入到dbGuest
xn.AppendChild(xe_u);
//保存XML文件
xd.Save(Server.MapPath("/xml/xml/doc.xml"));
fill();
}


private void Button4_Click(object sender, System.EventArgs e)
{//修改邮件地址动作
XmlDocument xd=new XmlDocument();
xd.Load(Server.MapPath(
"/xml/xml/doc.xml"));//载入XML文件
XmlNodeList xnl=xd.SelectSingleNode("dbGuest").ChildNodes;//获取dbGuest节点的全部子节


foreach(XmlNode xn in xnl)//迭代所有子节点
{
XmlElement xe
=(XmlElement)xn;//将迭代到的子节点转换成XmlElement类型
XmlNodeList xnl2=xe.GetElementsByTagName("Name");//返回所有Name列的数据
if(xnl2.Count>0)//如果大于零就表示有撒,
{
if(xnl2[0].InnerText==ddl.SelectedItem.Text)//如果该列有和被下拉列表中返回的

值一样的数据的话
{
XmlNodeList xnl3
=xe.ChildNodes;//再获取下面所有的子节点
foreach(XmlNode xn1 in xnl3)//再迭代子集
{
XmlElement xe2
=(XmlElement)xn1;//转换类型
if(xe2.Name=="Email")//如果节点名字是Email的话
{
xe2.InnerText
=tbn.Text;//该节点的值就替换成文本框中的
break;
}

}

break;
}

}

}

xd.Save(Server.MapPath(
"/xml/xml/doc.xml"));
fill();
}

}

}

xml文件
<? xml version = " 1.0 "  encoding = " utf-8 " ?>
< dbGuest >
< User >
< Name > 天轰穿 </ Name >
< City > 江油 </ City >
< Email > 7665 @fds.com </ Email >
< Time > 2006 - 5 - 6   16 : 10 : 56 </ Time >
</ User >
< User >
< Name > 田洪川 </ Name >
< City > 绵阳 </ City >
< Email > th@DFs.com </ Email >
< Time > 2006 - 5 - 6   16 : 06 : 51 </ Time >
</ User >
< User >
< Name > 黄小梅 </ Name >
< City > 南充 </ City >
< Email > 5543 @ds.com </ Email >
< Time > 2006 - 5 - 6   16 : 07 : 15 </ Time >
</ User >
</ dbGuest >  
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值