Mercurial > repos > IBBoard
annotate Xml/XmlTools.cs @ 24:5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
* Add methods to get an attribute value as an integer or double to core
author | IBBoard <dev@ibboard.co.uk> |
---|---|
date | Sat, 28 Mar 2009 20:35:43 +0000 |
parents | |
children | 148edabc9c73 |
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; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
7 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
8 namespace IBBoard.Xml |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
9 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
10 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
11 /// 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
|
12 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
13 public class XmlTools |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
14 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
15 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
16 /// 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
|
17 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
18 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
19 /// 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
|
20 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
21 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
22 /// 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
|
23 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
24 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
25 /// 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
|
26 /// </returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
27 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
|
28 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
29 try |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
30 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
31 return int.Parse(elem.GetAttribute(attributeName)); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
32 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
33 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
34 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
35 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
|
36 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
37 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
38 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
39 /// <summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
40 /// 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
|
41 /// </summary> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
42 /// <param name="elem"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
43 /// 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
|
44 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
45 /// <param name="attributeName"> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
46 /// 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
|
47 /// </param> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
48 /// <returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
49 /// 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
|
50 /// </returns> |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
51 private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
52 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
53 double doubleVal = double.NaN; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
54 string attribValue = elem.GetAttribute(attributeName); |
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 if (attribValue == "INF") |
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 doubleVal = double.PositiveInfinity; |
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 else |
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 try |
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 return int.Parse(attribValue); |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
65 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
66 catch(FormatException) |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
67 { |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
68 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
|
69 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
70 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
71 |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
72 return doubleVal; |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
73 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
74 } |
5cbf8bbf9b05
Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff
changeset
|
75 } |