annotate Xml/XmlTools.cs @ 27:4ea1fc351533

Re #18 - Migrate XML tools * Add method to get an attribute as a boolean
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 Mar 2009 20:54:01 +0000
parents 14f3daf48ba5
children c71855e241fc
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
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
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
17 /// Gets the value of an attribute of an element as a boolean. Throws a FormatException if the attribute is not a boolean.
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
18 /// </summary>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
19 /// <param name="elem">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
20 /// The <see cref="XmlElement"/> to get the attribute value of
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
21 /// </param>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
22 /// <param name="attributeName">
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
23 /// The name of the attribute to get as a boolean
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
24 /// </param>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
25 /// <returns>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
26 /// The value of the attribute as an boolean
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
27 /// </returns>
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
28 public static bool GetBoolValueFromAttribute(XmlElement elem, string attributeName)
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
29 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
30 try
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
31 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
32 return bool.Parse(elem.GetAttribute(attributeName));
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
33 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
34 catch(FormatException)
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
35 {
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
36 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid boolean", attributeName, elem.Name, elem.GetAttribute("id")));
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
37 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
38 }
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
39
4ea1fc351533 Re #18 - Migrate XML tools
IBBoard <dev@ibboard.co.uk>
parents: 26
diff changeset
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 }