Digg StumbleUpon LinkedIn YouTube Flickr Facebook Twitter RSS Reset

Convertir la valeur d’un attribut

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
    {
        ///
<summary>
        /// weakly typed
        /// </summary>
 
        ///
<param name="element"></param>
        ///
<param name="clé"></param>
        /// <returns></returns>
        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);
        }
   }
}

No comments yet.

Leave a Comment