Exemple #1
0
        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));
        }
Exemple #2
0
        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);
            }
        }
Exemple #3
0
        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);
            }
        }
Exemple #4
0
        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);
            }
        }
Exemple #5
0
        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);
            }
        }