1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192 |
using
System; using
System.Collections.Generic; using
System.Linq; using
System.Text; using
System.Xml; using
System.IO; using
System.Windows.Forms; namespace
MyVertion { class
XMLOperate { private
string m_configPath = Application.StartupPath + @"\DatabaseConfig.xml" ; private
string vertion = "1.0" ; private
string encoding = "UTF-8" ; private
string standalone = "no" ; private
string comment = "Database Config" ; private
XmlDocument xmlDoc = null ; private
static volatile XMLOperate XmlOp = null ; public
static XMLOperate GetInstance() { if
( null
== XmlOp) { XmlOp = new
XMLOperate( null ); } return
XmlOp; } public
string _Vertion { set { vertion = value; } get { return
vertion; } } public
string _Encoding { set { encoding = value; } get { return
encoding; } } public
string _Standalone { set { standalone = value; } get { return
standalone; } } public
string _Comment { set { comment = value; } get { return
comment; } } public
string _ConfigPath { get { return
m_configPath; } } #region 构造函数 public
XMLOperate( string
xmlPath) { if
(! string .IsNullOrEmpty(xmlPath)) { m_configPath = xmlPath; } xmlDoc = new
XmlDocument(); } #endregion //创建configxml文件 public
void CreateConfigXml() { xmlDoc.CreateXmlDeclaration(vertion, encoding, standalone); xmlDoc.CreateComment(comment); XmlElement rootEle = xmlDoc.CreateElement( "Connection" ); xmlDoc.AppendChild(rootEle); XmlElement ele = xmlDoc.CreateElement( "Server" ); rootEle.AppendChild(ele); ele = xmlDoc.CreateElement( "Instance" ); rootEle.AppendChild(ele); ele = xmlDoc.CreateElement( "Database" ); rootEle.AppendChild(ele); ele = xmlDoc.CreateElement( "UserName" ); rootEle.AppendChild(ele); //ele = xmlDoc.CreateElement("Password"); //rootEle.AppendChild(ele); ele = xmlDoc.CreateElement( "Vertion" ); rootEle.AppendChild(ele); xmlDoc.Save(m_configPath); //加入XML的声明段落,<?xml version="1.0" encoding="utf-8"?> // xmlDoc.LoadXml(@"<?xml version=""1.0"" encoding=""utf-8""?> // <Connection> // </Connection>"); // XmlNode root = xmlDoc.SelectSingleNode("Connection"); } //检查配置文件是否存在 public
bool IsExist() { return
File.Exists(m_configPath); } //保存、更改xml文件 public
bool UpdateConfigInfo( string
server, string
instance, string
database, string
username, string
password, string
version) { if
(!IsExist()) { return
false ; } XmlNode root = xmlDoc.SelectSingleNode( "Connection" ); XmlNode xnd = root.SelectSingleNode( "Server" ); xnd.InnerText = server; xnd = root.SelectSingleNode( "Instance" ); xnd.InnerText = instance; xnd = root.SelectSingleNode( "Database" ); xnd.InnerText = database; xnd = root.SelectSingleNode( "UserName" ); xnd.InnerText = username; //xnd = root.SelectSingleNode("Password"); //xnd.InnerText = password; xnd = root.SelectSingleNode( "Vertion" ); if
(! string .IsNullOrEmpty(version)) { xnd.InnerText = version; } xmlDoc.Save(m_configPath); return
true ; } /// <summary> /// 读配置文件 /// </summary> /// <returns></returns> public
string ReadConfigInfo( string
item) { if
(!IsExist()) { //MessageBox.Show("配置文件不存在!", "提示:", MessageBoxButtons.OK, MessageBoxIcon.Warning); return
null ; } else { xmlDoc.Load(m_configPath); XmlNode root = xmlDoc.SelectSingleNode( "Connection" ); XmlNode xnd = root.SelectSingleNode(item); return
xnd.InnerText; } } } } |
原文:http://www.cnblogs.com/shenchao/p/3673944.html