Convertir la valeur d’un attribut

Author:

Utilisation,de,la,classe,'XmlTextReader',pour,lire,un,document,XML
{filelink=16408}

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

namespace Mesexemples.com
{
    public static class ExtensionsXmlElement
    {
        ///

        /// weakly typed
        /// 

        ///

        ///

        /// 
        internal static object GetAttributeWithConversion(this XmlElement element, Type typeToConvertTo, string clé)
        {
            if (!element.HasAttribute(clé))
                throw new ArgumentOutOfRangeException(string.Format("The clé '{0}' Introuvable dans l'élément XML.", clé));

            var stringValue = element.GetAttribute(clé);

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

            return converter.ConvertFromString(stringValue);
        }
   }
}

Leave a Reply

Your email address will not be published. Required fields are marked *