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
|
using System;
using System.Globalization;
using System.Xml;
namespace MediaBrowser.Controller.Xml
{
public static class XmlExtensions
{
public static int SafeGetInt32(this XmlNode node)
{
return SafeGetInt32(node, 0);
}
public static int SafeGetInt32(this XmlNode node, int defaultInt)
{
if (node != null && node.InnerText.Length > 0)
{
int rval;
if (Int32.TryParse(node.InnerText, out rval))
{
return rval;
}
}
return defaultInt;
}
private static CultureInfo _usCulture = new CultureInfo("en-US");
public static float SafeGetSingle(this XmlNode rvalNode, float minValue, float maxValue)
{
if (rvalNode.InnerText.Length > 0)
{
float rval;
// float.TryParse is local aware, so it can be probamatic, force us culture
if (float.TryParse(rvalNode.InnerText, NumberStyles.AllowDecimalPoint, _usCulture, out rval))
{
if (rval >= minValue && rval <= maxValue)
{
return rval;
}
}
}
return minValue;
}
public static float SafeGetSingle(this XmlNode doc, string path, float minValue, float maxValue)
{
XmlNode rvalNode = doc.SelectSingleNode(path);
if (rvalNode != null)
{
rvalNode.SafeGetSingle(minValue, maxValue);
}
return minValue;
}
public static string SafeGetString(this XmlNode node)
{
return SafeGetString(node, null);
}
public static string SafeGetString(this XmlNode node, string defaultValue)
{
if (node != null && node.InnerText.Length > 0)
{
return node.InnerText;
}
return defaultValue;
}
}
}
|