1 | using System;
|
---|
2 | using System.Globalization;
|
---|
3 | using System.Xml;
|
---|
4 |
|
---|
5 | namespace Common
|
---|
6 | {
|
---|
7 | public static class XmlNodeExt
|
---|
8 | {
|
---|
9 | public static bool GetValueBool(this XmlNode xmlNode, string name, bool defaultValue)
|
---|
10 | {
|
---|
11 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
12 | if (node2 != null)
|
---|
13 | {
|
---|
14 | if (node2.InnerText.ToLowerInvariant() == "true") return true;
|
---|
15 | return false;
|
---|
16 | }
|
---|
17 | return defaultValue;
|
---|
18 | }
|
---|
19 |
|
---|
20 | public static string GetValueString(this XmlNode xmlNode, string name, string defaultValue)
|
---|
21 | {
|
---|
22 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
23 | if (node2 != null)
|
---|
24 | {
|
---|
25 | return node2.InnerText;
|
---|
26 | }
|
---|
27 | return defaultValue;
|
---|
28 | }
|
---|
29 |
|
---|
30 | public static int GetValueInt(this XmlNode xmlNode, string name, int defaultValue)
|
---|
31 | {
|
---|
32 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
33 | if (node2 != null)
|
---|
34 | {
|
---|
35 | if (int.TryParse(node2.InnerText, out int value))
|
---|
36 | {
|
---|
37 | return value;
|
---|
38 | }
|
---|
39 | return defaultValue;
|
---|
40 | }
|
---|
41 | return defaultValue;
|
---|
42 | }
|
---|
43 |
|
---|
44 | public static long GetValueLong(this XmlNode xmlNode, string name, long defaultValue)
|
---|
45 | {
|
---|
46 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
47 | if (node2 != null)
|
---|
48 | {
|
---|
49 | if (long.TryParse(node2.InnerText, out long value))
|
---|
50 | {
|
---|
51 | return value;
|
---|
52 | }
|
---|
53 | return defaultValue;
|
---|
54 | }
|
---|
55 | return defaultValue;
|
---|
56 | }
|
---|
57 |
|
---|
58 | public static float GetValueFloat(this XmlNode xmlNode, string name, float defaultValue)
|
---|
59 | {
|
---|
60 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
61 | if (node2 != null)
|
---|
62 | {
|
---|
63 | if (float.TryParse(node2.InnerText, out float value))
|
---|
64 | {
|
---|
65 | return value;
|
---|
66 | }
|
---|
67 | return defaultValue;
|
---|
68 | }
|
---|
69 | return defaultValue;
|
---|
70 | }
|
---|
71 |
|
---|
72 | public static double GetValueDouble(this XmlNode xmlNode, string name, double defaultValue)
|
---|
73 | {
|
---|
74 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
75 | if (node2 != null)
|
---|
76 | {
|
---|
77 | if (double.TryParse(node2.InnerText, out double value))
|
---|
78 | {
|
---|
79 | return value;
|
---|
80 | }
|
---|
81 | return defaultValue;
|
---|
82 | }
|
---|
83 | return defaultValue;
|
---|
84 | }
|
---|
85 |
|
---|
86 | public static TimeSpan GetValueTimeSpan(this XmlNode xmlNode, string name, TimeSpan defaultValue)
|
---|
87 | {
|
---|
88 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
89 | if (node2 != null)
|
---|
90 | {
|
---|
91 | return XmlConvert.ToTimeSpan(node2.InnerText);
|
---|
92 | }
|
---|
93 | return defaultValue;
|
---|
94 | }
|
---|
95 |
|
---|
96 | public static DateTime GetValueDateTime(this XmlNode xmlNode, string name, DateTime defaultValue)
|
---|
97 | {
|
---|
98 | XmlNode node2 = xmlNode.SelectSingleNode(name);
|
---|
99 | if (node2 != null)
|
---|
100 | {
|
---|
101 | return XmlConvert.ToDateTime(node2.InnerText, XmlDateTimeSerializationMode.RoundtripKind);
|
---|
102 | }
|
---|
103 | return defaultValue;
|
---|
104 | }
|
---|
105 |
|
---|
106 | public static void SetValueBool(this XmlNode xmlNode, string name, bool value)
|
---|
107 | {
|
---|
108 | if (xmlNode.OwnerDocument != null)
|
---|
109 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value.ToString();
|
---|
110 | }
|
---|
111 |
|
---|
112 | public static void SetValueString(this XmlNode xmlNode, string name, string value)
|
---|
113 | {
|
---|
114 | if (xmlNode.OwnerDocument != null && !string.IsNullOrEmpty(value))
|
---|
115 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value;
|
---|
116 | }
|
---|
117 |
|
---|
118 | public static void SetValueInt(this XmlNode xmlNode, string name, int value)
|
---|
119 | {
|
---|
120 | if (xmlNode.OwnerDocument != null)
|
---|
121 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value.ToString();
|
---|
122 | }
|
---|
123 |
|
---|
124 | public static void SetValueLong(this XmlNode xmlNode, string name, long value)
|
---|
125 | {
|
---|
126 | if (xmlNode.OwnerDocument != null)
|
---|
127 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value.ToString();
|
---|
128 | }
|
---|
129 |
|
---|
130 | public static void SetValueFloat(this XmlNode xmlNode, string name, float value)
|
---|
131 | {
|
---|
132 | if (xmlNode.OwnerDocument != null)
|
---|
133 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value.ToString(CultureInfo.InvariantCulture);
|
---|
134 | }
|
---|
135 |
|
---|
136 | public static void SetValueDouble(this XmlNode xmlNode, string name, double value)
|
---|
137 | {
|
---|
138 | if (xmlNode.OwnerDocument != null)
|
---|
139 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = value.ToString(CultureInfo.InvariantCulture);
|
---|
140 | }
|
---|
141 |
|
---|
142 | public static void SetValueTimeSpan(this XmlNode xmlNode, string name, TimeSpan value)
|
---|
143 | {
|
---|
144 | if (xmlNode.OwnerDocument != null && value != TimeSpan.Zero)
|
---|
145 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = XmlConvert.ToString(value);
|
---|
146 | }
|
---|
147 |
|
---|
148 | public static void SetValueDateTime(this XmlNode xmlNode, string name, DateTime value)
|
---|
149 | {
|
---|
150 | if (xmlNode.OwnerDocument != null && value != DateTime.MinValue)
|
---|
151 | xmlNode.AppendChild(xmlNode.OwnerDocument.CreateElement(name)).InnerText = XmlConvert.ToString(value, XmlDateTimeSerializationMode.RoundtripKind);
|
---|
152 | }
|
---|
153 | }
|
---|
154 | } |
---|