Get Attribute With Conversion : Attribute « XML « C# / C Sharp

Home
C# / C Sharp
1.2D Graphics
2.Class Interface
3.Collections Data Structure
4.Components
5.Data Types
6.Database ADO.net
7.Date Time
8.Design Patterns
9.Development Class
10.Event
11.File Stream
12.Generics
13.GUI Windows Form
14.Internationalization I18N
15.Language Basics
16.LINQ
17.Network
18.Office
19.Reflection
20.Regular Expressions
21.Security
22.Services Event
23.Thread
24.Web Services
25.Windows
26.Windows Presentation Foundation
27.XML
28.XML LINQ
C# Book
C# / C Sharp by API
C# / CSharp Tutorial
C# / CSharp Open Source
C# / C Sharp » XML » AttributeScreenshots 
Get Attribute With Conversion
 
//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>
        /// weakly typed
        /// </summary>
        /// <param name="element"></param>
        /// <param name="key"></param>
        /// <returns></returns>
        internal static object GetAttributeWithConversion(this XmlElement element, Type typeToConvertTo, string key)
        {
            if (!element.HasAttribute(key))
                throw new ArgumentOutOfRangeException(string.Format("The key '{0}' cannot be found in xml element.", key));

            var stringValue = element.GetAttribute(key);

            var converter = System.ComponentModel.TypeDescriptor.GetConverter(typeToConvertTo);

            return 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
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.Attempts to get and convert an attribute from an XmlElement
15.Gets a string from an attribute or node.
16.Remove all the xml namespaces (xmlns) attributes in the xml string
17.Find a single Attribute of the type 'attributeType' from the supplied class
w___w_w.j___a___v_a__2_s___._c_om__ | Contact Us
Copyright 2009 - 12 Demo Source and Support. All rights reserved.
All other trademarks are property of their respective owners.