




版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、1.建立一个class,ReadWriteConfig.csusing System;using System.Configuration;using System.Reflection;using System.Web;using System.Xml;public enum ConfigFileType WebConfig, AppConfignamespace WebApplication1
2、160; /*/ <summary> / Summary description for ReadWriteConfig. / </summary> public class ReadWriteConfig
3、; public string docName = String.Empty; private XmlNode node=null; private int _configTyp
4、e; public int ConfigType get return _configType;
5、 set _configType=value; SetValue#region SetValue public bool SetValue(string key, strin
6、g value) XmlDocument cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc);
7、160; / retrieve the appSettings node node = cfgDoc.SelectSingleNode("/appSettings");
8、0; if( node = null )
9、60;throw new InvalidOperationException( "appSettings section not found"); try
10、0; / XPath select setting "add" element that contains this k
11、ey XmlElement addElem= (XmlElement)node.SelectSingleNode("/addkey='" +key +"'")
12、 if(addElem!=null) a
13、ddElem.SetAttribute("value",value); / not found, so we&
14、#160;need to add the element, key and value else
15、160; XmlElement entry = cfgDoc.CreateElement("add");
16、160;entry.SetAttribute("key",key); entry.SetAttribute("value",value);
17、160; node.AppendChild(entry);
18、160; /save it saveConfigDoc(cfgDoc,docName); return true;
19、160; catch
20、60; return false; #endregio
21、n saveConfigDoc#region saveConfigDoc private void saveConfigDoc(XmlDocument cfgDoc,string cfgDocPath)
22、160; try XmlTextWriter writer = new XmlTextWriter( cfgDocPath ,
23、;null ); writer.Formatting = Formatting.Indented; cfgDoc.WriteTo(
24、 writer ); writer.Flush(); writer.Close();
25、0; return; catch
26、0; throw;
27、0; #endregion removeElement#region removeElement public bool removeElement (string elementKey)
28、0; try XmlDocument
29、160;cfgDoc = new XmlDocument(); loadConfigDoc(cfgDoc); / retrieve t
30、he appSettings node node = cfgDoc.SelectSingleNode("/appSettings");
31、 if( node = null ) throw new Inv
32、alidOperationException( "appSettings section not found");
33、60; / XPath select setting "add" element that contains this key to remove node.RemoveChild( no
34、de.SelectSingleNode("/addkey='" +elementKey +"'") ); saveConfigDoc(cfgDoc,docName); &
35、#160; return true; catch
36、60; return false;
37、60; #endregion modifyElement#region modifyElement public bool modifyElement (string elementKey)
38、60; try XmlDocument cfgDoc =
39、new XmlDocument(); loadConfigDoc(cfgDoc); / retrieve the appSettings
40、0;node node = cfgDoc.SelectSingleNode("/appSettings");
41、;if( node = null ) throw new InvalidOperationException(
42、 "appSettings section not found"); / XPath
43、160;select setting "add" element that contains this key to remove node.RemoveChild(node.SelectSingleNode("/ad
44、dkey='" +elementKey +"'"); saveConfigDoc(cfgDoc,docName); r
45、eturn true; catch
46、60; return false; #endregion
47、; loadConfigDoc#region loadConfigDoc private XmlDocument loadConfigDoc( XmlDocument cfgDoc )
48、 / load the config file if( Convert.ToInt32(ConfigType)=Convert.ToInt32(ConfigFileType.AppConfig)
49、 docName= (Assembly.GetEntryAssembly().GetName().Name;
50、160; docName += ".exe.config" else
51、160; cfgDoc.Load( docName ); return cfgDoc;
52、0; #endregion 2.建立测试页面2.1 html<HTML> <HEAD> <title>WebForm1</title>
53、60; <meta name="GENERATOR" Content="Microsoft Visual Studio .NET 7.1"> <meta name="CODE_LANGUAGE" Content="C#">
54、60; <meta name="vs_defaultClientScript" content="JavaScript"> <meta name="vs_targetSchema" content </HEAD> <body MS_POSITIONING=&
55、quot;GridLayout"> <form id="Form1" method="post" runat="server"> <asp:Button id="Button1" style=&quo
56、t;Z-INDEX: 101; LEFT: 296px; POSITION: absolute; TOP: 120px" runat="server" Text="删除"></asp:Button>
57、160; <asp:Button id="Button2" style="Z-INDEX: 102; LEFT: 232px; POSITION: absolute; TOP: 120px" runat="server"
58、 Text="新增"></asp:Button> <asp:Label id="Label1" style="Z-INDEX: 103; LEFT: 104px; POSITION: absolute; TOP: 32px" r
59、unat="server">key</asp:Label> <asp:TextBox id="TextBox1" style="Z-INDEX: 104; LEFT: 168px; POSITION: absolute; TOP: 32px" runat="ser
60、ver"></asp:TextBox> <asp:Label id="Label2" style="Z-INDEX: 105; LEFT: 104px; POSITION: absolute; TOP: 96px" runat="server">value&
61、lt;/asp:Label> <asp:TextBox id="TextBox2" style="Z-INDEX: 106; LEFT: 168px; POSITION: absolute; TOP: 80px" runat="server"></asp:TextBox&g
62、t; <asp:Button id="Button3" style="Z-INDEX: 107; LEFT: 360px; POSITION: absolute; TOP: 120px" runat="server"
63、160; Text="修改"></asp:Button> </form> </body></HTML>2.2 cs代码public class
64、0; protected protected protected protected protected protect
65、ed protected private void Page_Load(object sender, System.EventArgs e)
66、60; Web Form Designer generated code#region Web Form Designer generated code overri
67、de protected void OnInit(EventArgs e) / / CODEGEN: This call is r
68、equired by the ASP.NET Web Form Designer. / InitializeComponent();
69、160; base.OnInit(e); /*/ <summary> / Required method for Designer sup
70、port - do not modify / the contents of this method with the code editor. / </summary>
71、60;private void InitializeComponent() this.Button1.Click += new System.EventHandler(this.Button1_Click);
72、0; this.Button2.Click += new System.EventHandler(this.Button2_Click); this.Button3.Click += new System.EventHandler(this.Button3_Click); this
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 2026中国华能集团有限公司校园招聘备考考试题库附答案解析
- 2026贵州贵阳市观山湖区外国语实验中学面向公费师范生招聘8人考试参考题库及答案解析
- 武汉文化馆施工方案
- 2025广东柳州市融安县发展和改革局二层事业单位编外人员招聘1人模拟试卷及完整答案详解
- 2025事业单位中巴车考试题及答案
- 2025事业单位招聘会计笔试题目及答案
- 2025事业单位英语题目及答案
- 数字财政决算方案范本
- 垃圾分类评估方案范本
- 2025年政府采购考试题库及答案
- 水资源开发与保护联合协议
- 社区康复服务在医疗旅游中的价值体现
- 受托代理投标合同
- 《急性心肌梗死急诊》课件
- 2025年保洁员(高级)理论考试题(附答案)
- 中心幼儿园第三季度意识形态分析研判报告-
- 广东省六校2025届高三第二次联考数学试卷含解析
- 2022年199管综真题及答案解析版
- 2025届高考作文素材积累:高中五册课本素材及运用
- 施工方案和安全技术方案的版本
- 《设备管理台账》
评论
0/150
提交评论