changeset 62:6e46a62ad9b8

Re #25: Add method for parsing enums from XML elements * Add two methods for parsing enums from XML elements and returning cast results
author IBBoard <dev@ibboard.co.uk>
date Mon, 30 Nov 2009 20:46:54 +0000
parents 972cc51adeeb
children 8fe11cd7d3bf
files Xml/XmlTools.cs
diffstat 1 files changed, 17 insertions(+), 0 deletions(-) [+]
line wrap: on
line diff
--- a/Xml/XmlTools.cs	Fri Nov 06 19:57:34 2009 +0000
+++ b/Xml/XmlTools.cs	Mon Nov 30 20:46:54 2009 +0000
@@ -102,6 +102,23 @@
 			
 			return doubleVal;
 		}
+		
+		public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName)
+		{
+			return GetEnumValueFromAttribute<T>(elem, attributeName, true);
+		}
+		
+		public static T GetEnumValueFromAttribute<T>(XmlElement elem, string attributeName, bool ignoreCase)
+		{
+			try
+			{
+				return (T) Enum.Parse(typeof (T), elem.GetAttribute(attributeName), ignoreCase);
+			}
+			catch(ArgumentException)
+			{
+				throw new FormatException(String.Format("Attribute '{0}' of {1} with ID {2} was not a valid {3} enum", attributeName, elem.Name, elem.GetAttribute("id"), typeof(T).Name));
+			}
+		}
 
 		private static NumberFormatInfo GetNumberFormatInfo()
 		{