XML文件的操作

1、按照节点方式操作

  1 #region Pay站点配置
  2         private void InitComProtocolPay()
  3         {
  4             this.comProtocolPay.Items.Add("http");
  5             this.comProtocolPay.Items.Add("https");
  6         }
  7         private void InitServiceComProtocolPay()
  8         {
  9             this.comServiceProcPay.Items.Add("http");
 10             this.comServiceProcPay.Items.Add("https");
 11         }
 12         XmlDocument doc2 = null;
 13         XmlDocument docApp2 = null;
 14 
 15         #region 读取数据库配置
 16         public void readConfigConnPay()
 17         {
 18 
 19             if (File.Exists(XmlPath2))
 20             {
 21 
 22                 doc2 = new XmlDocument();
 23                 doc2.Load(XmlPath2);
 24                 XmlNode xnserver = doc2.SelectSingleNode("configuration/connectionStrings/add");
 25                 if (xnserver.Attributes["name"].InnerText.Equals("OracleConnectionString"))
 26                 {
 27                     string constr = xnserver.Attributes["connectionString"].InnerText;
 28                     string s = "=";
 29                     string e = ";";
 30 
 31                     string[] dbgroup = constr.Split(';');
 32                     if (dbgroup != null && dbgroup.Length > 0)
 33                     {
 34                         string dbname = dbgroup[0] + ";";
 35                         //求得strtempa 和 strtempb 出现的位置: 
 36                         int IndexofA = dbname.IndexOf(s);
 37                         int IndexofB = dbname.IndexOf(e);
 38                         txtDatanamePay.Text = dbname.Substring(IndexofA + 1, IndexofB - IndexofA - 1);
 39                         string dbpwd = dbgroup[1] + ";";
 40                         int IndexofA1 = dbpwd.IndexOf(s);
 41                         int IndexofB1 = dbpwd.IndexOf(e);
 42                         txtDBPwdPay.Text = dbpwd.Substring(IndexofA1 + 1, IndexofB1 - IndexofA1 - 1);
 43                         string dbuser = dbgroup[2] + ";";
 44                         int IndexofA2 = dbuser.IndexOf(s);
 45                         int IndexofB2 = dbuser.IndexOf(e);
 46                         txtDBUserPay.Text = dbuser.Substring(IndexofA2 + 1, IndexofB2 - IndexofA2 - 1);
 47                     }
 48                 }
 49             }
 50             else
 51             {
 52                 MessageBox.Show("请检查是否存在配置文件");
 53                 //throw new Exception("没有该文件");
 54             }
 55         }
 56         #endregion
 57         #region  读取Appsetting配置
 58         public void ReadAppsetingPay()
 59         {
 60             try
 61             {
 62                 docApp2 = new XmlDocument();
 63                 docApp2.Load(XmlPath2);
 64                 XmlNodeList nodes = docApp2.SelectNodes("configuration/appSettings/add");
 65                 foreach (XmlNode node in nodes)
 66                 {
 67                     var type = node.Attributes["key"].Value;
 68                     switch (type)
 69                     {
 70                         case "tsmurl":
 71                             string tsmurl = node.Attributes["value"].Value;
 72                             string tsmIP = "";
 73                             string tsmPort = "";
 74                             if (!string.IsNullOrEmpty(tsmurl))
 75                             {
 76                                 tsmIP = tsmurl.Split(':')[1].Substring(2, tsmurl.Split(':')[1].Length - 2);
 77                                 tsmPort = tsmurl.Split(':')[2].Substring(0, tsmurl.Split(':')[2].Length - 1);
 78                             }
 79                             comProtocolPay.Text = tsmurl.Split(':')[0];
 80                             txtTSMIPPay.Text = tsmIP;
 81                             txtTSMPortPay.Text = tsmPort;
 82                             break;
 83                         case "tskey":
 84                             txtTekeyPay.Text = node.Attributes["value"].Value;
 85                             break;
 86                         case "ts3des":
 87                             txtTs3desPay.Text = node.Attributes["value"].Value;
 88                             break;
 89                         case "ml":
 90                             txtmlPay.Text = node.Attributes["value"].Value;
 91                             break;
 92                         case "noticenum":
 93                             txtnoticenum1Pay.Text = node.Attributes["value"].Value;
 94                             break;
 95                         case "SocketUrl":
 96                             txtSocketUrlPay.Text = node.Attributes["value"].Value;
 97                             break;
 98                         case "SocketWwUrl":
 99                             txtSocketWwUrl1Pay.Text = node.Attributes["value"].Value;
100                             break;
101                         case "ispky":
102                             txtIspkyPay.Text = node.Attributes["value"].Value;
103                             break;
104                         case "isjypass":
105                             txtisjypassPay.Text = node.Attributes["value"].Value;
106                             break;
107                         case "isqrcode":
108                             txtisqrcode1Pay.Text = node.Attributes["value"].Value;
109                             break;
110                         case "pkv":
111                             txtkpyPay.Text = node.Attributes["value"].Value;
112                             break;
113                         case "version":
114                             txtversionPay.Text = node.Attributes["value"].Value;
115                             break;
116                         case "zktoaccount":
117                             txtzktoaccount1Pay.Text = node.Attributes["value"].Value;
118                             break;
119                         case "vtitle":
120                             txtvtitlePay.Text = node.Attributes["value"].Value;
121                             break;
122                         case "ServiceHall":
123                             txtServiceHallPay.Text = node.Attributes["value"].Value;
124                             break;
125                         case "serviceUrl":
126                             string serviceURL = node.Attributes["value"].Value;
127                             string serviceIP = "";
128                             string servicePort = "";
129                             if (!string.IsNullOrEmpty(serviceURL))
130                             {
131                                 serviceIP = serviceURL.Split(':')[1].Substring(2, serviceURL.Split(':')[1].Length - 2);
132                                 servicePort = serviceURL.Split(':')[2].Substring(0, serviceURL.Split(':')[2].Length - 1);
133                             }
134                             comServiceProcPay.Text = serviceURL.Split(':')[0];
135                             txtserviceIPPay.Text = serviceIP;
136                             txtservicePortPay.Text = servicePort;
137                             break;
138                         case "islikeorderid":
139                             txtislikeorderid1Pay.Text = node.Attributes["value"].Value;
140                             break;
141 
142                     }
143                 }
144             }
145             catch (Exception ex)
146             {
147                 throw ex;
148             }
149 
150 
151         }
152         #endregion
153 
154         #region 保存连接串
155         public void DoSaveConnPay()
156         {
157             XmlNodeList nodes = doc2.SelectNodes("/configuration/connectionStrings/add");
158 
159             foreach (XmlNode node in nodes)
160             {
161                 if (node.Attributes["name"].InnerText == "OracleConnectionString")
162                 {
163                     string constr = node.Attributes["connectionString"].InnerText;
164                     string s = "=";
165                     string e = ";";
166                     string strdbminpool = "";
167                     string strdbmaxpool = "";
168                     string strdblefttime = "";
169                     string[] dbgroup = constr.Split(';');
170                     if (dbgroup != null && dbgroup.Length > 0)
171                     {
172                         string dbminpool = dbgroup[3] + ";";
173                         int IndexofA3 = dbminpool.IndexOf(s);
174                         int IndexofB3 = dbminpool.IndexOf(e);
175                         strdbminpool = dbminpool.Substring(IndexofA3 + 1, IndexofB3 - IndexofA3 - 1);
176                         string dbmaxpool = dbgroup[4] + ";";
177                         int IndexofA4 = dbmaxpool.IndexOf(s);
178                         int IndexofB4 = dbmaxpool.IndexOf(e);
179                         strdbmaxpool = dbmaxpool.Substring(IndexofA4 + 1, IndexofB4 - IndexofA4 - 1);
180                         string dblefttime = dbgroup[5] + ";";
181                         int IndexofA5 = dblefttime.IndexOf(s);
182                         int IndexofB5 = dblefttime.IndexOf(e);
183                         strdblefttime = dblefttime.Substring(IndexofA5 + 1, IndexofB5 - IndexofA5 - 1);
184                     }
185 
186                     string dbstr = string.Format("Data Source={0};password={1};user id={2};min pool size={3};max pool size={4};Connection Lifetime={5}", txtDatanamePay.Text, txtDBPwdPay.Text, txtDBUserPay.Text, strdbminpool, strdbmaxpool, strdblefttime);
187                     node.Attributes["connectionString"].Value = dbstr;
188                 }
189             }
190         }
191         #endregion
192         #region 保存Appseting串
193         public void DoSaveAppSetingPay()
194         {
195             XmlNodeList nodes = doc2.SelectNodes("/configuration/appSettings/add");
196 
197             foreach (XmlNode node in nodes)
198             {
199                 var type = node.Attributes["key"].Value;
200                 switch (type)
201                 {
202                     case "tsmurl":
203                         string tsmurl = comProtocolPay.Text + "://" + txtTSMIPPay.Text + ":" + txtTSMPortPay.Text + "/";
204                         node.Attributes["value"].Value = tsmurl;
205                         break;
206                     case "tskey":
207                         node.Attributes["value"].Value = txtTekeyPay.Text;
208                         break;
209                     case "ts3des":
210                         node.Attributes["value"].Value = txtTs3desPay.Text;
211                         break;
212 
213                     case "ml":
214                         node.Attributes["value"].Value = txtmlPay.Text;
215                         break;
216                     case "noticenum":
217                         node.Attributes["value"].Value = txtnoticenum1Pay.Text;
218                         break;
219                     case "SocketUrl":
220                         node.Attributes["value"].Value = txtSocketUrlPay.Text;
221                         break;
222                     case "SocketWwUrl":
223                         node.Attributes["value"].Value = txtSocketWwUrl1Pay.Text;
224                         break;
225                     case "ispky":
226                         node.Attributes["value"].Value = txtIspkyPay.Text;
227                         break;
228                     case "isjypass":
229                         node.Attributes["value"].Value = txtisjypassPay.Text;
230                         break;
231                     case "isqrcode":
232                         node.Attributes["value"].Value = txtisqrcode1Pay.Text;
233                         break;
234                     case "pkv":
235                         node.Attributes["value"].Value = txtkpyPay.Text;
236                         break;
237                     case "version":
238                         node.Attributes["value"].Value = txtversionPay.Text;
239                         break;
240                     case "zktoaccount":
241                         node.Attributes["value"].Value = txtzktoaccount1Pay.Text;
242                         break;
243                     case "vtitle":
244                         node.Attributes["value"].Value = txtvtitlePay.Text;
245                         break;
246                     case "ServiceHall":
247                         node.Attributes["value"].Value = txtServiceHallPay.Text;
248                         break;
249                     case "serviceUrl":
250                         string serviceStr = comServiceProcPay.Text + "://" + txtserviceIPPay.Text + ":" + txtservicePortPay.Text + "/";
251                         node.Attributes["value"].Value = serviceStr;
252                         break;
253                     case "islikeorderid":
254                         node.Attributes["value"].Value = txtislikeorderid1Pay.Text;
255                         break;
256                 }
257             }
258         }
259         #endregion
260         private void buttonPaySave_Click(object sender, EventArgs e)
261         {
262             try
263             {
264                 DoSaveConnPay();
265                 DoSaveAppSetingPay();
266                 doc2.Save(XmlPath2);
267                 MessageBox.Show("保存成功!");
268                 //this.Close();
269             }
270             catch (Exception)
271             {
272                 MessageBox.Show("保存失败!");
273             }
274         }
275         #endregion
View Code

2、按照元素方式操作

 1 #region 赋值配置文件路径
 2         /// <summary>
 3         /// 返回配置文件路径
 4         /// p1:SynjonesPay.Web
 5         /// p2:SynjonesPay.Pay
 6         /// p3:SynjonesPay.Merr
 7         /// p4:SynjonesPay.Manager
 8         /// </summary>
 9         /// <returns></returns>
10         public static void getConfigPath()
11         {
12             try
13             {
14                 string dir = AppDomain.CurrentDomain.BaseDirectory;
15                 string path = Path.GetFullPath(@"....").Replace("\", "/");
16                 if (File.Exists(@"" + path + "/Manager/Web.config"))
17                 {
18                     XmlPath1 = @"" + path + "/Manager/Web.config";
19                 }
20                 if (File.Exists(@"" + path + "/Pay/Web.config"))
21                 {
22                     XmlPath2 = @"" + path + "/Pay/Web.config";
23                 }
24                 if (File.Exists(@"" + path + "/Merr/Web.config"))
25                 {
26                     XmlPath3 = @"" + path + "/Merr/Web.config";
27                 }
28                 if (File.Exists(@"" + path + "/Web/Web.config"))
29                 {
30                     XmlPath4 = @"" + path + "/Web/Web.config";
31                 }
32                 #region 
33                 //if (File.Exists(XmlPath))
34                 //{
35                 //    XmlDocument xmlDocument = new XmlDocument();
36                 //    xmlDocument.Load(XmlPath);
37                 //    //SynjonesInstallAll/Systems/manager
38                 //    //foreach (XmlNode node in xmlDocument.SelectNodes("SynjonesInstallAll/Systems/manager"))
39                 //    //{
40                 //    //    string p1 = node.SelectSingleNode("RootPath").InnerText;
41                 //    //    XmlPath1 = @"" + p1 + "Web.config";
42                 //    //}
43                 //    //foreach (XmlNode node in xmlDocument.SelectNodes("SynjonesInstallAll/Systems/pay"))
44                 //    //{
45                 //    //    string p2 = node.SelectSingleNode("RootPath").InnerText;
46                 //    //    XmlPath2 = @"" + p2 + "Web.config";
47                 //    //}
48                 //    //foreach (XmlNode node in xmlDocument.SelectNodes("SynjonesInstallAll/Systems/merr"))
49                 //    //{
50                 //    //    string p3 = node.SelectSingleNode("RootPath").InnerText;
51                 //    //    XmlPath3 = @"" + p3 + "Web.config";
52                 //    //}
53                 //    //foreach (XmlNode node in xmlDocument.SelectNodes("SynjonesInstallAll/Systems/web"))
54                 //    //{
55                 //    //    string p4 = node.SelectSingleNode("RootPath").InnerText;
56                 //    //    XmlPath4 = @"" + p4 + "Web.config";
57                 //    //}
58 
59 
60                 //}
61                 //else
62                 //{
63                 //    MessageBox.Show("请检查是否存在配置文件路径的配置文件!");
64                 //}
65                 #endregion
66             }
67             catch (Exception ex)
68             {
69                 throw new Exception(ex.ToString() + "Main:getConfigPathNotFound");
70             }
71            
72 
73 
74         }
75         #endregion
View Code
原文地址:https://www.cnblogs.com/gyjjyg/p/7098478.html