Mercurial > repos > IBBoard
comparison 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 |
comparison
equal
deleted
inserted
replaced
23:fb4fdab841db | 24:5cbf8bbf9b05 |
---|---|
1 // This file (XmlTools.cs) is a part of the IBBoard library and is copyright 2009 IBBoard | |
2 // | |
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. | |
4 // | |
5 | |
6 using System; | |
7 | |
8 namespace IBBoard.Xml | |
9 { | |
10 /// <summary> | |
11 /// Some basic tools for handling XML files and retrieving their values | |
12 /// </summary> | |
13 public class XmlTools | |
14 { | |
15 /// <summary> | |
16 /// Gets the value of an attribute of an element as an integer. Throws a FormatException if the attribute is not an integer. | |
17 /// </summary> | |
18 /// <param name="elem"> | |
19 /// The <see cref="XmlElement"/> to get the attribute value of | |
20 /// </param> | |
21 /// <param name="attributeName"> | |
22 /// The name of the attribute to get as an integer | |
23 /// </param> | |
24 /// <returns> | |
25 /// The value of the attribute as an integer | |
26 /// </returns> | |
27 public static int GetIntValueFromAttribute(XmlElement elem, string attributeName) | |
28 { | |
29 try | |
30 { | |
31 return int.Parse(elem.GetAttribute(attributeName)); | |
32 } | |
33 catch(FormatException) | |
34 { | |
35 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); | |
36 } | |
37 } | |
38 | |
39 /// <summary> | |
40 /// Gets the value of an attribute of an element as a double. Throws a FormatException if the attribute is not a double. | |
41 /// </summary> | |
42 /// <param name="elem"> | |
43 /// The <see cref="XmlElement"/> to get the attribute value of | |
44 /// </param> | |
45 /// <param name="attributeName"> | |
46 /// The name of the attribute to get as a double | |
47 /// </param> | |
48 /// <returns> | |
49 /// The value of the attribute as an double | |
50 /// </returns> | |
51 private double GetDoubleValueFromAttribute(XmlElement elem, string attributeName) | |
52 { | |
53 double doubleVal = double.NaN; | |
54 string attribValue = elem.GetAttribute(attributeName); | |
55 | |
56 if (attribValue == "INF") | |
57 { | |
58 doubleVal = double.PositiveInfinity; | |
59 } | |
60 else | |
61 { | |
62 try | |
63 { | |
64 return int.Parse(attribValue); | |
65 } | |
66 catch(FormatException) | |
67 { | |
68 throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid number", attributeName, elem.Name, elem.GetAttribute("id"))); | |
69 } | |
70 } | |
71 | |
72 return doubleVal; | |
73 } | |
74 } | |
75 } |