-
Notifications
You must be signed in to change notification settings - Fork 1
/
XMLSetttingsFileManager.cs
544 lines (425 loc) · 17.6 KB
/
XMLSetttingsFileManager.cs
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
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
using System;
using System.Collections.Generic;
using System.Text;
namespace PDX.BTS.DataMaintenance.MaintTools
{
// Set the GUID that will be used when this Library is referenced by other applications. Specifying a
// GUID insures that the library will maintain binary compatibility through all build operations.
[System.Runtime.InteropServices.Guid("A353E472-6946-4c50-BD89-7374232CCBD3")]
public class XMLSetttingsFileManager
{
public void XMLFile(string XMLFilePath)
{
// Set the Path to the INI File.
_filePath = XMLFilePath;
} // XMLFile
public string ReadUserSetting(string SectionName, string EntryName)
{
string settingValue = null;
// Set the Default Value to an empty string and call the overload of this method that
// requires that a default value to read the value from the Settings File.
settingValue = ReadUserSetting(SectionName, EntryName, "");
// Return the value that was retrieved from the Settings File to the calling routine.
return settingValue;
} // ReadUserSetting
public string ReadUserSetting(string SectionName, string EntryName, string DefaultValue)
{
//AMS.Profile.Xml xmlFile = null;
//object objectValue = null;
string section = SectionName;
VerifyAndAdjustSection(ref section);
string entry = EntryName;
VerifyAndAdjustEntry(ref entry);
try
{
// Open the XML Document.
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
// Retrieve the ROOT location in the XML Document.
System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlNode entryNode = root.SelectSingleNode(GetSectionsPath(section) + "/" + GetEntryPath(entry));
return entryNode.InnerText;
}
catch
{
// Return a NULL String to the calling routine to indicate that this process failed.
return null;
}
} // readUserSetting
public void WriteUserSetting(string SectionName, string EntryName, string EntryValue)
{
// Make sure all of the necessary information was passed to this routine before attempting to write to
// the Settings File.
if (SectionName == null || SectionName == "" || EntryName == null || EntryName == "" || EntryValue == "")
{
// Exit the Method.
return;
}
try
{
// If the value is null, remove the entry
if (EntryValue == null)
{
RemoveUserSetting(SectionName, EntryValue);
return;
}
// Verify that the file is available for reading and that the XML Section and Entry Name values are valid.
VerifyNotReadOnly();
string section = SectionName;
VerifyAndAdjustSection(ref section);
string entry = EntryName;
VerifyAndAdjustEntry(ref entry);
// If the File does not exist, create it.
if (System.IO.File.Exists(_filePath))
{
System.Xml.XmlTextWriter writer = new System.Xml.XmlTextWriter(_filePath, System.Text.Encoding.UTF8);
writer.Formatting = System.Xml.Formatting.Indented;
writer.WriteStartDocument();
writer.WriteStartElement("profile");
writer.WriteStartElement("section");
writer.WriteAttributeString("name", null, section);
writer.WriteStartElement("entry");
writer.WriteAttributeString("name", null, entry);
writer.WriteString(EntryValue);
writer.WriteEndElement();
writer.WriteEndElement();
writer.WriteEndElement();
writer.Close();
// Exit the method now that the file has been created.
return;
}
// The file exists, edit it
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
System.Xml.XmlElement root = doc.DocumentElement;
// Get the section element and add it if it's not there
System.Xml.XmlNode sectionNode = root.SelectSingleNode(GetSectionsPath(section));
if (sectionNode == null)
{
System.Xml.XmlElement element = doc.CreateElement("section");
System.Xml.XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = section;
element.Attributes.Append(attribute);
sectionNode = root.AppendChild(element);
}
// Get the entry element and add it if it's not there
System.Xml.XmlNode entryNode = sectionNode.SelectSingleNode(GetEntryPath(entry));
if (entryNode == null)
{
System.Xml.XmlElement element = doc.CreateElement("entry");
System.Xml.XmlAttribute attribute = doc.CreateAttribute("name");
attribute.Value = entry;
element.Attributes.Append(attribute);
entryNode = sectionNode.AppendChild(element);
}
// Add the value and save the file
entryNode.InnerText = EntryValue;
doc.Save(_filePath);
}
catch
{
// Exit this Method.
return;
}
} // WriteUserSetting
/// <summary>
/// Removes an entry from a section. </summary>
/// <param name="section">
/// The name of the section that holds the entry. </param>
/// <param name="entry">
/// The name of the entry to remove. </param>
/// <exception cref="InvalidOperationException">
/// <see cref="Profile.Name" /> is null or empty or
/// <see cref="Profile.ReadOnly" /> is true. </exception>
/// <exception cref="ArgumentNullException">
/// Either section or entry is null. </exception>
/// <exception cref="XmlException">
/// Parse error in the XML being loaded from the file or
/// the resulting XML document would not be well formed. </exception>
/// <remarks>
/// The <see cref="Profile.Changing" /> event is raised before removing the entry.
/// If its <see cref="ProfileChangingArgs.Cancel" /> property is set to true, this method
/// returns immediately without removing the entry. After the entry has been removed,
/// the <see cref="Profile.Changed" /> event is raised.
/// <para>
/// Note: If <see cref="XmlBased.Buffering" /> is enabled, the entry is not removed from the
/// XML file until the buffer is flushed (or closed). </para></remarks>
/// <seealso cref="RemoveUserSection" />
public void RemoveUserSetting(string section, string entry)
{
VerifyNotReadOnly();
VerifyAndAdjustSection(ref section);
VerifyAndAdjustEntry(ref entry);
// Verify the document exists
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
if (doc == null)
return;
// Get the entry's node, if it exists
System.Xml.XmlElement root = doc.DocumentElement;
System.Xml.XmlNode entryNode = root.SelectSingleNode(GetSectionsPath(section) + "/" + GetEntryPath(entry));
if (entryNode == null)
return;
entryNode.ParentNode.RemoveChild(entryNode);
doc.Save(_filePath);
}
/// <summary>
/// Removes a section. </summary>
/// <param name="section">
/// The name of the section to remove. </param>
/// <exception cref="InvalidOperationException">
/// <see cref="Profile.Name" /> is null or empty or
/// <see cref="Profile.ReadOnly" /> is true. </exception>
/// <exception cref="ArgumentNullException">
/// section is null. </exception>
/// <exception cref="XmlException">
/// Parse error in the XML being loaded from the file or
/// the resulting XML document would not be well formed. </exception>
/// <remarks>
/// The <see cref="Profile.Changing" /> event is raised before removing the section.
/// If its <see cref="ProfileChangingArgs.Cancel" /> property is set to true, this method
/// returns immediately without removing the section. After the section has been removed,
/// the <see cref="Profile.Changed" /> event is raised.
/// <para>
/// Note: If <see cref="XmlBased.Buffering" /> is enabled, the section is not removed from the
/// XML file until the buffer is flushed (or closed). </para></remarks>
/// <seealso cref="RemoveUserSetting" />
public void RemoveUserSection(string section)
{
VerifyNotReadOnly();
VerifyAndAdjustSection(ref section);
// Verify the document exists
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
if (doc == null)
return;
// Get the root node, if it exists
System.Xml.XmlElement root = doc.DocumentElement;
if (root == null)
return;
// Get the section's node, if it exists
System.Xml.XmlNode sectionNode = root.SelectSingleNode(GetSectionsPath(section));
if (sectionNode == null)
return;
root.RemoveChild(sectionNode);
doc.Save(_filePath);
}
public bool UserSectionExists(string SectionName)
{
// Make sure a valid Section Name was passed to this Method before moving on.
if (SectionName == null || SectionName == "")
{
// Exit this Method.
return false;
}
try
{
// Retrieve the Array of Section Names from the XML Settings File.
string[] sectionNames = GetSectionNames();
// Determine if the section exists.
bool itExists = false;
foreach(string currentSectionName in sectionNames)
{
if (System.String.Compare(currentSectionName, SectionName, System.StringComparison.CurrentCultureIgnoreCase) == 0)
{
// The Section exists so set the itExists indicator to TRUE.
itExists = true;
}
}
// Return the indicator of whether or not the section exists.
return itExists;
}
catch
{
// Return FALSE to the calling routine to indicate that the Section could not be found.
return false;
}
} // UserSectionExists
public bool UserSettingExists(string SectionName, string EntryName)
{
// Make sure a valid Section Name and Entry Name were passed to this Method before moving on.
if (SectionName == null || SectionName == "" || EntryName == null || EntryName == "")
{
// Exit this Method.
return false;
}
try
{
// Retrieve the List of Entry Names in the specified Section in the XML Settings File.
string[] sectionEntryNames = GetEntryNames(SectionName);
// Determine if the specified Entry Name is in the list of returned entry names.
bool itExists = false;
foreach (string currentEntryName in sectionEntryNames)
{
// If the Current Entry Name matches the specified entry name, set the itExists indicator to TRUE.
if (System.String.Compare(currentEntryName, EntryName, System.StringComparison.CurrentCultureIgnoreCase) == 0)
{
// Set the Indicator to TRUE.
itExists = true;
}
}
// Return the indicator of whether or not the entry name exists.
return itExists;
}
catch
{
// Return FALSE to the calling routine to indicate that the Entry could not be found in the Settings File.
return false;
}
} // UserSettingExists
/// <summary>
/// Retrieves the names of all the sections. </summary>
/// <returns>
/// If the XML file exists, the return value is an array with the names of all the sections;
/// otherwise it's null. </returns>
/// <exception cref="InvalidOperationException">
/// <see cref="Profile.Name" /> is null or empty. </exception>
/// <exception cref="XmlException">
/// Parse error in the XML being loaded from the file. </exception>
/// <seealso cref="Profile.HasSection" />
/// <seealso cref="GetEntryNames" />
public string[] GetSectionNames()
{
// Verify the document exists
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
if (doc == null)
return null;
// Get the root node, if it exists
System.Xml.XmlElement root = doc.DocumentElement;
if (root == null)
return null;
// Get the section nodes
System.Xml.XmlNodeList sectionNodes = root.SelectNodes("section[@name]");
if (sectionNodes == null)
return null;
// Add all section names to the string array
string[] sections = new string[sectionNodes.Count];
int i = 0;
foreach (System.Xml.XmlNode node in sectionNodes)
sections[i++] = node.Attributes["name"].Value;
// Return the Array of Section Names to the calling method.
return sections;
} // GetSectionNames()
/// <summary>
/// Retrieves the names of all the entries inside a section. </summary>
/// <param name="section">
/// The name of the section holding the entries. </param>
/// <returns>
/// If the section exists, the return value is an array with the names of its entries;
/// otherwise it's null. </returns>
/// <exception cref="InvalidOperationException">
/// <see cref="Profile.Name" /> is null or empty. </exception>
/// <exception cref="ArgumentNullException">
/// section is null. </exception>
/// <exception cref="XmlException">
/// Parse error in the XML being loaded from the file. </exception>
/// <seealso cref="Profile.HasEntry" />
/// <seealso cref="GetSectionNames" />
public string[] GetEntryNames(string section)
{
// Verify the section exists
if (!UserSectionExists(section))
return null;
VerifyAndAdjustSection(ref section);
System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
doc.Load(_filePath);
if (doc == null)
return null;
// Get the root node, if it exists
System.Xml.XmlElement root = doc.DocumentElement;
if (root == null)
return null;
// Get the entry nodes
System.Xml.XmlNodeList entryNodes = root.SelectNodes(GetSectionsPath(section) + "/entry[@name]");
if (entryNodes == null)
return null;
// Add all entry names to the string array
string[] entries = new string[entryNodes.Count];
int i = 0;
foreach (System.Xml.XmlNode node in entryNodes)
entries[i++] = node.Attributes["name"].Value;
// Return the Array of Entry Names to the calling method.
return entries;
} // GetEntryNames()
public string XMLSettingsFile
{
get
{
return _filePath;
}
set
{
_filePath = value;
}
} // XMLSettingsFile - Property
/// <summary>
/// Retrieves the XPath string used for retrieving a section from the XML file. </summary>
/// <returns>
/// An XPath string. </returns>
/// <seealso cref="GetEntryPath" />
private string GetSectionsPath(string section)
{
return "section[@name=\"" + section + "\"]";
}
/// <summary>
/// Retrieves the XPath string used for retrieving an entry from the XML file. </summary>
/// <returns>
/// An XPath string. </returns>
/// <seealso cref="GetSectionsPath" />
private string GetEntryPath(string entry)
{
return "entry[@name=\"" + entry + "\"]";
}
/// <summary>
/// Verifies the given section name is not null and trims it. </summary>
/// <param name="section">
/// The section name to verify and adjust. </param>
/// <exception cref="ArgumentNullException">
/// section is null. </exception>
/// <remarks>
/// This method may be used by derived classes to make sure that a valid
/// section name has been passed, and to make any necessary adjustments to it
/// before passing it to the corresponding APIs. </remarks>
/// <seealso cref="VerifyAndAdjustEntry" />
protected void VerifyAndAdjustSection(ref string section)
{
if (section == null)
throw new ArgumentNullException("section");
section = section.Trim();
}
/// <summary>
/// Verifies the given entry name is not null and trims it. </summary>
/// <param name="entry">
/// The entry name to verify and adjust. </param>
/// <remarks>
/// This method may be used by derived classes to make sure that a valid
/// entry name has been passed, and to make any necessary adjustments to it
/// before passing it to the corresponding APIs. </remarks>
/// <exception cref="ArgumentNullException">
/// entry is null. </exception>
/// <seealso cref="VerifyAndAdjustSection" />
protected void VerifyAndAdjustEntry(ref string entry)
{
if (entry == null)
throw new ArgumentNullException("entry");
entry = entry.Trim();
}
/// <summary>
/// Verifies the ReadOnly property is not true. </summary>
/// <remarks>
/// This method may be used by derived classes as a convenient way to
/// validate that modifications to the profile can be made. </remarks>
/// <exception cref="InvalidOperationException">
/// ReadOnly is true. </exception>
/// <seealso cref="ReadOnly" />
private void VerifyNotReadOnly()
{
System.IO.FileInfo xmlFileInfo = new System.IO.FileInfo(_filePath);
if (xmlFileInfo.IsReadOnly)
throw new InvalidOperationException("Operation not allowed because ReadOnly property is true.");
}
// Define a Field to hold the Path and File Name of the XML File.
string _filePath = null;
} // Class: XMLSettingsFileManager
} // NameSpace: PDX.BTS.DataMaintenance.MaintTools