forked from colgreen/sharpneat
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathXmlUtils.cs
162 lines (147 loc) · 5.55 KB
/
XmlUtils.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
/* ***************************************************************************
* This file is part of SharpNEAT - Evolution of Neural Networks.
*
* Copyright 2004-2016 Colin Green ([email protected])
*
* SharpNEAT is free software; you can redistribute it and/or modify
* it under the terms of The MIT License (MIT).
*
* You should have received a copy of the MIT License
* along with SharpNEAT; if not, see https://opensource.org/licenses/MIT.
*/
using System;
using System.Xml;
namespace SharpNeat.Domains
{
/// <summary>
/// Static helper methods for reading value from XML configuration data in DOM form.
/// </summary>
public class XmlUtils
{
/// <summary>
/// Parse the inner text of element with the given name as an integer. If element is missing or parsing fails then
/// throws an ArgumentException.
/// </summary>
public static int GetValueAsInt(XmlElement xmlParent, string elemName)
{
int? val = TryGetValueAsInt(xmlParent, elemName);
if(null == val) {
throw new ArgumentException($"Missing [{elemName}] configuration setting.");
}
return val.Value;
}
/// <summary>
/// Parse the inner text of element with the given name as an integer. If element is missing or parsing fails then
/// returns null.
/// </summary>
public static int? TryGetValueAsInt(XmlElement xmlParent, string elemName)
{
XmlElement xmlElem = xmlParent.SelectSingleNode(elemName) as XmlElement;
if(null == xmlElem) {
return null;
}
string valStr = xmlElem.InnerText;
if(string.IsNullOrEmpty(valStr)) {
return null;
}
int result;
if(int.TryParse(valStr, out result))
{
return result;
}
return null;
}
/// <summary>
/// Parse the inner text of element with the given name as a double. If element is missing or parsing fails then
/// throws an ArgumentException.
/// </summary>
public static double GetValueAsDouble(XmlElement xmlParent, string elemName)
{
double? val = TryGetValueAsDouble(xmlParent, elemName);
if(null == val) {
throw new ArgumentException($"Missing [{elemName}] configuration setting.");
}
return val.Value;
}
/// <summary>
/// Parse the inner text of element with the given name as a double. If element is missing or parsing fails then
/// returns null.
/// </summary>
public static double? TryGetValueAsDouble(XmlElement xmlParent, string elemName)
{
XmlElement xmlElem = xmlParent.SelectSingleNode(elemName) as XmlElement;
if(null == xmlElem) {
return null;
}
string valStr = xmlElem.InnerText;
if(string.IsNullOrEmpty(valStr)) {
return null;
}
double result;
if(double.TryParse(valStr, out result))
{
return result;
}
return null;
}
/// <summary>
/// Parse the inner text of element with the given name as a boolean. If element is missing or parsing fails then
/// throws an ArgumentException.
/// </summary>
public static bool GetValueAsBool(XmlElement xmlParent, string elemName)
{
bool? val = TryGetValueAsBool(xmlParent, elemName);
if(null == val) {
throw new ArgumentException($"Missing [{elemName}] configuration setting.");
}
return val.Value;
}
/// <summary>
/// Parse the inner text of element with the given name as a boolean. If element is missing or parsing fails then
/// returns null.
/// </summary>
public static bool? TryGetValueAsBool(XmlElement xmlParent, string elemName)
{
XmlElement xmlElem = xmlParent.SelectSingleNode(elemName) as XmlElement;
if(null == xmlElem) {
return null;
}
string valStr = xmlElem.InnerText;
if(string.IsNullOrEmpty(valStr)) {
return null;
}
bool result;
if(bool.TryParse(valStr, out result))
{
return result;
}
return null;
}
/// <summary>
/// Read the inner text of element with the given name. If element is missing then throws an ArgumentException.
/// </summary>
public static string GetValueAsString(XmlElement xmlParent, string elemName)
{
string val = TryGetValueAsString(xmlParent, elemName);
if(null == val) {
throw new ArgumentException($"Missing [{elemName}] configuration setting.");
}
return val;
}
/// <summary>
/// Read the inner text of element with the given name. If element is missing then returns null.
/// </summary>
public static string TryGetValueAsString(XmlElement xmlParent, string elemName)
{
XmlElement xmlElem = xmlParent.SelectSingleNode(elemName) as XmlElement;
if(null == xmlElem) {
return null;
}
string valStr = xmlElem.InnerText;
if(string.IsNullOrEmpty(valStr)) {
return null;
}
return valStr;
}
}
}