1
using System;
2
using System.Collections;
3
using System.ComponentModel;
4
using System.Data;
5
using System.Drawing;
6
using System.Web;
7
using System.Web.SessionState;
8
using System.Web.UI;
9
using System.Web.UI.WebControls;
10
using System.Web.UI.HtmlControls;
11
using System.Xml;
12
using System.Xml.XPath;
13
14
namespace OperateXML
15
{
16
///
17
/// xdoc 的摘要說明。
18
///
19
public class XML : System.Web.UI.Page
20
{
21
protected System.Web.UI.WebControls.Label Label1;
22
protected System.Web.UI.WebControls.Label Label2;
23
protected System.Web.UI.WebControls.Button Button1;
24
protected System.Web.UI.WebControls.Label Label3;
25
protected System.Web.UI.WebControls.Button Button2;
26
protected System.Web.UI.WebControls.Button Button3;
27
protected System.Web.UI.WebControls.DataGrid dg;
28
protected System.Web.UI.WebControls.DropDownList ddl;
29
protected System.Web.UI.WebControls.TextBox tbn;
30
protected System.Web.UI.WebControls.Label Label4;
31
protected System.Web.UI.WebControls.Label Label5;
32
protected System.Web.UI.WebControls.Label Label6;
33
protected System.Web.UI.WebControls.TextBox tbna;
34
protected System.Web.UI.WebControls.TextBox tbc;
35
protected System.Web.UI.WebControls.TextBox tbe;
36
protected System.Web.UI.WebControls.Button Button4;
37
38
private void Page_Load(object sender, System.EventArgs e)
39
{
40
if(!Page.IsPostBack)
41
{
42
fill();
43
}
44
}
45
46
Web 窗體設計器生成的代碼#region Web 窗體設計器生成的代碼
47
override protected void OnInit(EventArgs e)
48
{
49
//
50
// CODEGEN: 該調用是 ASP.NET Web 窗體設計器所必需的。
51
//
52
InitializeComponent();
53
base.OnInit(e);
54
}
55
56
///
57
/// 設計器支援所需的方法 - 不要使用代碼編輯器修改
58
/// 此方法的内容。
59
///
60
private void InitializeComponent()
61
{
62
this.Button1.Click += new System.EventHandler(this.Button1_Click);
63
this.Button2.Click += new System.EventHandler(this.Button2_Click);
64
this.Button3.Click += new System.EventHandler(this.Button3_Click);
65
this.Button4.Click += new System.EventHandler(this.Button4_Click);
66
this.Load += new System.EventHandler(this.Page_Load);
67
68
}
69
#endregion
70
71
綁定#region 綁定
72
73
private void fill()
74
{
75
DataSet ds=new DataSet();
76
ds.ReadXml(Server.MapPath("//OperateXML//doc.xml"));
77
dg.DataSource=ds.Tables[0];
78
dg.DataBind();
79
XmlDocument xd=new XmlDocument();
80
xd.Load(Server.MapPath("//OperateXML//doc.xml"));
81
System.Xml.XmlNodeList xnl=xd.GetElementsByTagName("Name");
82
ddl.Items.Clear();
83
for(int i=0;i<xnl.Count;i++)
84
{
85
ddl.Items.Add(xnl[i].InnerText);
86
}
87
}
88
89
#endregion
90
91
查詢#region 查詢
92
93
private void Button1_Click(object sender, System.EventArgs e)
94
{
95
XmlDocument xd=new XmlDocument();
96
xd.Load(Server.MapPath("//OperateXML//doc.xml"));
97
tbn.Text=xd.SelectSingleNode("//User[Name='"+ddl.SelectedItem.Text+"']").ChildNodes.Item(2).InnerText.ToString();
98
99
}
100
101
#endregion
102
103
删除#region 删除
104
private void Button2_Click(object sender, System.EventArgs e)
105
{
106
XmlDocument xdoc=new XmlDocument();
107
xdoc.Load(Server.MapPath("//OperateXML//doc.xml"));
108
XmlNodeList xnl=xdoc.SelectSingleNode("dbGuest").ChildNodes;
109
110
foreach(XmlNode xn in xnl)
111
{
112
XmlElement xe=(XmlElement)xn;
113
XmlNodeList node=xe.GetElementsByTagName("Name");//查找他的Name行
114
if(node.Count>0)
115
{
116
if(node[0].InnerText==ddl.SelectedItem.Text)//如果目前節點的名字和下拉清單傳來的一樣,就删除
117
{
118
xe.RemoveAll();//删除節點的所有指定屬性和子集,但是不删除預設屬性,我很郁悶,不知道怎麼連屬性一起删除,如果有朋友知道,請一定告訴我下,謝謝了!
119
120
}
121
break;
122
}
123
124
}
125
xdoc.Save(Server.MapPath("//OperateXML//doc.xml"));
126
fill();
127
}
128
129
#endregion
130
131
添加#region 添加
132
133
private void Button3_Click(object sender, System.EventArgs e)
134
{
135
string name=this.tbna.Text.ToString();
136
string city=this.tbc.Text.ToString();
137
string email=this.tbe.Text.ToString();
138
139
XmlDocument xd=new XmlDocument();
140
xd.Load(Server.MapPath("//OperateXML//doc.xml"));
141
//查找dbGuest節點
142
XmlNode xn=xd.SelectSingleNode("dbGuest");
143
//建立user節點
144
XmlElement xe_u=xd.CreateElement("User");
145
//建立一個name元素
146
XmlElement xe_a1=xd.CreateElement("Name");
147
xe_a1.InnerText=name;//設定該元素的值
148
xe_u.AppendChild(xe_a1);//把剛才建立的元素插入name節點裡面去
149
XmlElement xe_a2=xd.CreateElement("City");
150
xe_a2.InnerText=city;
151
xe_u.AppendChild(xe_a2);
152
XmlElement xe_a3=xd.CreateElement("Email");
153
xe_a3.InnerText=email;
154
xe_u.AppendChild(xe_a3);
155
XmlElement xe_a4=xd.CreateElement("Time");
156
xe_a4.InnerText=(DateTime.Now).ToString();
157
xe_u.AppendChild(xe_a4);
158
//将User節點插入到dbGuest
159
xn.AppendChild(xe_u);
160
//儲存XML檔案
161
xd.Save(Server.MapPath("//OperateXML//doc.xml"));
162
fill();
163
}
164
165
#endregion
166
167
修改郵件位址#region 修改郵件位址
168
169
private void Button4_Click(object sender, System.EventArgs e)
170
{//修改郵件位址
171
XmlDocument xd=new XmlDocument();
172
xd.Load(Server.MapPath("//OperateXML//doc.xml"));//載入XML檔案
173
XmlNodeList xnl=xd.SelectSingleNode("dbGuest").ChildNodes;//擷取dbGuest節點的全部子節點
174
foreach(XmlNode xn in xnl)//疊代所有子節點
175
{
176
XmlElement xe=(XmlElement)xn;//将疊代到的子節點轉換成XmlElement類型
177
XmlNodeList xnl2=xe.GetElementsByTagName("Name");//傳回所有Name列的資料
178
if(xnl2.Count>0)//如果大于零就表示有撒,
179
{
180
if(xnl2[0].InnerText==ddl.SelectedItem.Text)//如果該列有和被下拉清單中傳回的值一樣的資料的話
181
{
182
XmlNodeList xnl3=xe.ChildNodes;//再擷取下面所有的子節點
183
foreach(XmlNode xn1 in xnl3)//再疊代子集
184
{
185
XmlElement xe2=(XmlElement)xn1;//轉換類型
186
if(xe2.Name=="Email")//如果節點名字是Email的話
187
{
188
xe2.InnerText=tbn.Text;//該節點的值就替換成文本框中的
189
break;
190
}
191
}
192
break;
193
}
194
}
195
}
196
xd.Save(Server.MapPath("//OperateXML//doc.xml"));
197
fill();
198
}
199
200
#endregion
201
}
202
}