- 01
- 02
- 03
- 04
- 05
- 06
- 07
- 08
- 09
- 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
private static T GetElementValue<T>(this XmlElement elm, string elementName, T defaultValue = default(T), bool throwIfError = false, bool throwIfMissing = false)
where T : IConvertible
{
string val = GetElementValue(elm, elementName);
if (string.IsNullOrEmpty(val) == false)
{
if (typeof(T) == typeof(string))
{
return (T)(object)val;
}
if (typeof(T) == typeof(bool))
{
return (T)(object)(val == "1");
}
try
{
if (typeof(T) == typeof(DateTime))
{
return (T)(object)DateTime.Parse(val, System.Globalization.CultureInfo.InvariantCulture); ;
}
return (T)Convert.ChangeType(val, typeof(T), CultureInfo.InvariantCulture);
}
catch (Exception exc)
{
if (throwIfError)
throw exc;
}
}
if (throwIfMissing)
throw new ArgumentNullException("The parameter '" + elementName + "' is missing");
return defaultValue;
}