annotate Xml/XmlTools.cs @ 26:14f3daf48ba5

Re #18 - Migrate XML handling methods to core utils * Make double parsing method public
author IBBoard <dev@ibboard.co.uk>
date Sat, 28 Mar 2009 20:37:40 +0000
parents 148edabc9c73
children 4ea1fc351533
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>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
17 /// 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
18 /// </summary>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
19 /// <param name="elem">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
20 /// 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
21 /// </param>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
22 /// <param name="attributeName">
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
23 /// 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
24 /// </param>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
25 /// <returns>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
26 /// 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
27 /// </returns>
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
28 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
29 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
30 try
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
31 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
32 return int.Parse(elem.GetAttribute(attributeName));
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
33 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
34 catch(FormatException)
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
35 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
36 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
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
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
40 /// <summary>
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 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
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 a double
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 double
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
51 /// </returns>
26
14f3daf48ba5 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents: 25
diff changeset
52 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
53 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
54 double doubleVal = double.NaN;
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
55 string attribValue = elem.GetAttribute(attributeName);
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
56
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
57 if (attribValue == "INF")
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
58 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
59 doubleVal = double.PositiveInfinity;
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
60 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
61 else
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 try
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
64 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
65 return int.Parse(attribValue);
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
66 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
67 catch(FormatException)
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
68 {
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
69 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
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
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
73 return doubleVal;
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 }
5cbf8bbf9b05 Re #18 - Migrate XML handling methods to core utils
IBBoard <dev@ibboard.co.uk>
parents:
diff changeset
76 }