Attempts to get and convert an attribute from an XmlElement : Attribute « XML « C# / C Sharp






Attempts to get and convert an attribute from an XmlElement

  


//http://validationframework.codeplex.com/
//License:  Microsoft Permissive License (Ms-PL) v1.1  
using System;
using System.Xml;

namespace ValidationFramework.Extensions
{
    public static class XmlElementExtensions
    {
        /// <summary>
        /// Attempts to get and convert an attribute from a <see cref="XmlElement"/>. If the item does not exist or can't be converted a <paramref name="defaultValue"/> is returned.
        /// </summary>
        /// <typeparam name="T">The <see cref="Type"/> to try to convert to.</typeparam>
        /// <param name="element">The <see cref="XmlElement"/> to extract the attribute value from.</param>
        /// <param name="key">The key to use or the extraction</param>
        /// <param name="defaultValue">The default value if <paramref name="key"/> is not found.</param>
        /// <returns>The value from <paramref name="attributes"/> if <paramref name="key"/> exists; otherwise <paramref name="defaultValue"/>.</returns>
        internal static T GetAttribute<T>(this XmlElement element, string key, T defaultValue)
        {
            if (!element.HasAttribute(key))
                return defaultValue;

            var stringValue = element.GetAttribute(key);

            var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeof(T));

            if (!converter.CanConvertFrom(typeof(string)))
                return defaultValue;

            return (T)converter.ConvertFromString(stringValue);
        }


    }
}

   
    
  








Related examples in the same category

1.Set Attribute Value
2.Get Attribute Value
3.Get Int value from xml attribute
4.Get Attribute value (2)
5.Get attribute or return default value
6.Get Bool Attribute Or Throw exception
7.Get attribute value and convert to integer type
8.Get attribute value and convert to integer type or throw exception
9.Get Attribute and return Int64
10.Get Attribute and return Int64 or throw exception
11.Adds an XmlAttribute to a XmlNode
12.Get boolean value if an attribute is 1 or true
13.Attempts to get and cast attribute from an XmlElement
14.Gets a string from an attribute or node.
15.Remove all the xml namespaces (xmlns) attributes in the xml string
16.Get Attribute With Conversion
17.Find a single Attribute of the type 'attributeType' from the supplied class
18.Create Xml Attribute, Node
19.Create Attribute, Text Element, Image Element,Read Image Element
20.Attribute Util
21.Find and get Attribute value
22.Finds an attribute with the given name in the given XML node and returns the attribute of the value.
23.Parse Attribute
24.Get attribute value (3)
25.Get int type attribute value
26.Get attribute value or throw exception
27.Read an XML file from the xml element and the xml attribute
28.Create and set attribute
29.Adds a new attribute to the given target element.
30.Get Text Attribute
31.Get Int Attribute