using System; using System.IO; using System.Xml; using System.Web; using System.ComponentModel; using System.Collections.Generic; namespace Common.Infrastructure.Extensions { public static class XmlExtensions { public static IEnumerable Pluck(this XmlNodeList @this, string attributeName) { var list = new List(); if (@this.Count == 0) return list; foreach (XmlNode node in @this) { string attribValue = node.Attr(attributeName); if (String.IsNullOrEmpty(attribValue)) continue; list.Add(attribValue); } return list; } public static string XmlDecode(this XmlNode @this, string attributeName) { string value = Attr(@this, attributeName); if (String.IsNullOrEmpty(value)) return value; return HttpUtility.HtmlDecode(value); } public static string Attr(this XmlNode @this, string attributeName) { return Attr(@this, attributeName); } public static T Attr(this XmlNode @this, string attributeName) { if (@this == null || @this.Attributes[attributeName] == null || String.IsNullOrEmpty(@this.Attributes[attributeName].Value)) return default(T); return ChangeType(@this.Attributes[attributeName].Value); } public static object ChangeType(object value, Type conversionType) { // Note: This if block was taken from Convert.ChangeType as is, and is needed here since we're // checking properties on conversionType below. if (conversionType == null) { throw new ArgumentNullException("conversionType"); } // end if // If it's not a nullable type, just pass through the parameters to Convert.ChangeType if (conversionType.IsGenericType && conversionType.GetGenericTypeDefinition().Equals(typeof(Nullable<>))) { // It's a nullable type, so instead of calling Convert.ChangeType directly which would throw a // InvalidCastException (per http://weblogs.asp.net/pjohnson/archive/2006/02/07/437631.aspx), // determine what the underlying type is // If it's null, it won't convert to the underlying type, but that's fine since nulls don't really // have a type--so just return null // Note: We only do this check if we're converting to a nullable type, since doing it outside // would diverge from Convert.ChangeType's behavior, which throws an InvalidCastException if // value is null and conversionType is a value type. if (value == null) { return null; } // end if // It's a nullable type, and not null, so that means it can be converted to its underlying type, // so overwrite the passed-in conversion type with this underlying type NullableConverter nullableConverter = new NullableConverter(conversionType); conversionType = nullableConverter.UnderlyingType; } // end if // Now that we've guaranteed conversionType is something Convert.ChangeType can handle (i.e. not a // nullable type), pass the call on to Convert.ChangeType return Convert.ChangeType(value, conversionType); } public static TResult ChangeType(object value) { return (TResult)ChangeType(value, typeof(TResult)); } } }