Mercurial > repos > IBBoard
annotate Xml/XmlTools.cs @ 33:8971a1c48dbf
Re #18 - Add rounding to half methods
* Add Floor method that rounds down to a half
* Add Ceiling method that rounds up to a half
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sun, 17 May 2009 19:22:53 +0000 |
parents | 4ea1fc351533 |
children | c71855e241fc |
rev | line source |
---|---|
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
1 // This file (XmlTools.cs) is a part of the IBBoard library and is copyright 2009 IBBoard |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
2 // |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
3 // The file and the library/program it is in are licensed under the GNU LGPL license, either version 3 of the License or (at your option) any later version. Please see COPYING.LGPL for more information and the full license. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
4 // |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
5 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
6 using System; |
25
148edabc9c73
Re #18 - Migrate XML methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
24
diff
changeset
|
7 using System.Xml; |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 namespace IBBoard.Xml |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
12 /// Some basic tools for handling XML files and retrieving their values |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 public class XmlTools |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 /// <summary> |
27 | 17 /// Gets the value of an attribute of an element as a boolean. Throws a FormatException if the attribute is not a boolean. |
18 /// </summary> | |
19 /// <param name="elem"> | |
20 /// The <see cref="XmlElement"/> to get the attribute value of | |
21 /// </param> | |
22 /// <param name="attributeName"> | |
23 /// The name of the attribute to get as a boolean | |
24 /// </param> | |
25 /// <returns> | |
26 /// The value of the attribute as an boolean | |
27 /// </returns> | |
28 public static bool GetBoolValueFromAttribute(XmlElement elem, string attributeName) | |
29 { | |
30 try | |
31 { | |
32 return bool.Parse(elem.GetAttribute(attributeName)); | |
33 } | |
34 catch(FormatException) | |
35 { | |
36 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id"))); | |
37 } | |
38 } | |
39 | |
40 /// <summary> | |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
41 /// Gets the value of an attribute of an element as an integer. Throws a FormatException if the attribute is not an integer. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
44 /// The <see cref="XmlElement"/> to get the attribute value of |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
47 /// The name of the attribute to get as an integer |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
50 /// The value of the attribute as an integer |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 /// </returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 public static int GetIntValueFromAttribute(XmlElement elem, string attributeName) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 try |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
55 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
56 return int.Parse(elem.GetAttribute(attributeName)); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
57 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
58 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
59 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
60 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
61 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
62 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
63 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
64 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 /// Gets the value of an attribute of an element as a double. Throws a FormatException if the attribute is not a double. |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 /// The <see cref="XmlElement"/> to get the attribute value of |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
69 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 /// The name of the attribute to get as a double |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 /// The value of the attribute as an double |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 /// </returns> |
26
14f3daf48ba5
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
25
diff
changeset
|
76 public static double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) |
24
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
77 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
78 double doubleVal = double.NaN; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
79 string attribValue = elem.GetAttribute(attributeName); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
80 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
81 if (attribValue == "INF") |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
82 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
83 doubleVal = double.PositiveInfinity; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
84 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
85 else |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
86 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
87 try |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
88 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
89 return int.Parse(attribValue); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
90 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
91 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
92 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
93 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
94 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
95 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
96 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
97 return doubleVal; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
98 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
99 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
100 } |