public static String GetAttributeValueAsString(this XElement xel, String attributeName) { xel.CheckNotNull(); XAttribute xat = xel.Attribute(attributeName); xat.CheckValueNotNullOrWhitespace(); // XML stores new lines as \n return(xat.Value.Replace(Environment.NewLine, "\n") .Replace("\r", "\n") .Replace("\n", Environment.NewLine)); }
private static T GetAttributeValue <T>(this XElement xel, String attributeName) where T : struct, IConvertible { xel.CheckNotNull(); XAttribute xat = xel.Attribute(attributeName); xat.CheckValueNotNullOrWhitespace(); try { return(( T )Convert.ChangeType(xat.Value, typeof(T), XML_CULTURE)); } catch (Exception ex) { throw new ApplicationException("Could not convert attribute value '" + xat.Value + "' of attribute '" + attributeName + "' into type '" + typeof(T).FullName + "'!", ex); } }
public static Version GetAttributeValueAsVersion(this XElement xel, String attributeName) { xel.CheckNotNull(); XAttribute xat = xel.Attribute(attributeName); xat.CheckValueNotNullOrWhitespace(); try { return(Version.Parse(xat.Value)); } catch (Exception ex) { throw new ApplicationException("Could not convert attribute value '" + xat.Value + "' of attribute '" + xat.Name.LocalName + "' into type '" + typeof(Version).FullName + "'!", ex); } }
public static DateTime GetAttributeValueAsDateTime(this XElement xel, String attributeName) { xel.CheckNotNull(); XAttribute xat = xel.Attribute(attributeName); xat.CheckValueNotNullOrWhitespace(); try { return(DateTime.Parse(xat.Value, CultureInfo.InvariantCulture, DateTimeStyles.AdjustToUniversal)); } catch (Exception ex) { throw new ApplicationException("Could not convert attribute value '" + xat.Value + "' of attribute '" + xat.Name.LocalName + "' into type '" + typeof(DateTime).FullName + "'!", ex); } }
public static T GetAttributeValueAsEnum <T>(this XElement xel, String attributeName) { if (!typeof(T).IsEnum) { throw new ArgumentException("T must be an enumerated type!"); } xel.CheckNotNull(); XAttribute xat = xel.Attribute(attributeName); xat.CheckValueNotNullOrWhitespace(); try { return(( T )Enum.Parse(typeof(T), xat.Value)); } catch (Exception ex) { throw new ApplicationException("Could not convert attribute value '" + xat.Value + "' of attribute '" + xat.Name.LocalName + "' into enum '" + typeof(T).FullName + "'!", ex); } }