Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles / Languages / C#4.0

Using Extension Methods To Avoid XML Problems

3.40/5 (3 votes)
18 Jul 2014CPOL2 min read 9.1K  
This is an alternative for Using Extension Methods To Avoid XML Problems

Introduction

This is an alternative to JSOP's XML extension methods, which uses the System.Linq.Expressions namespace to dynamically generate reasonably efficient methods for converting elements and attributes to different types.

Background

The XElement and XAttribute classes provide explicit cast operators for 25 built-in types, which will handle most situations. However, there is no simple way to use these operators with a generic type parameter. There is also no built-in support for parsing types which expose a suitable TryParse method.

Enough Waffle, Give Me the Codes Already!

Here's a wall of code. Explanations are below. :)

C#
public static class XmlExtensions
{
    private static readonly Type[] PrimitiveTypes =
    {
        typeof(bool), 
        typeof(int), typeof(uint), 
        typeof(long), typeof(ulong),
        typeof(float), typeof(double), typeof(decimal),
        typeof(DateTime), typeof(DateTimeOffset),
        typeof(TimeSpan), typeof(Guid)
    };
    
    private static readonly Type[] NullableTypes =
    {
        typeof(string), typeof(bool?), 
        typeof(int?), typeof(uint?), 
        typeof(long?), typeof(ulong?),
        typeof(float?), typeof(double?), typeof(decimal?),
        typeof(DateTime?), typeof(DateTimeOffset?),
        typeof(TimeSpan?), typeof(Guid?)
    };
    
    private static Expression<Func<TSource, TValue, TValue>> BuildConverter<TSource, TValue>()
    {
        var pElement = Expression.Parameter(typeof(TSource), "el");
        var pDefaultValue = Expression.Parameter(typeof(TValue), "defaultValue");
        
        Expression body;
        if (PrimitiveTypes.Contains(pDefaultValue.Type))
        {
            Type nullableType = typeof(Nullable<>).MakeGenericType(pDefaultValue.Type);
            var value = Expression.Convert(pElement, nullableType);
            body = Expression.Coalesce(value, pDefaultValue);
        }
        else if (NullableTypes.Contains(pDefaultValue.Type))
        {
            var value = Expression.Convert(pElement, pDefaultValue.Type);
            body = Expression.Coalesce(value, pDefaultValue);
        }
        else
        {
            Type[] parameterTypes = { typeof(string), pDefaultValue.Type.MakeByRefType() };
            var tryParseMethod = pDefaultValue.Type.GetMethod("TryParse",
                BindingFlags.Public | BindingFlags.Static, null, 
                parameterTypes, null);
            
            if (tryParseMethod == null) 
            {
                throw new MissingMethodException(pDefaultValue.Type.FullName, "TryParse");
            }
            
            var returnStatement = Expression.Label(pDefaultValue.Type, "ret");
            
            var value = Expression.Variable(pDefaultValue.Type, "value");
            var stringValue = Expression.Convert(pElement, typeof(string));
            var tryParseResult = Expression.Call(tryParseMethod, stringValue, value);
            
            var returnParsed = Expression.IfThenElse(
                tryParseResult,
                Expression.Return(returnStatement, value, pDefaultValue.Type),
                Expression.Return(returnStatement, pDefaultValue, pDefaultValue.Type));
                
            var returnValue = Expression.IfThenElse(
                Expression.Equal(stringValue, Expression.Constant(null, typeof(string))),
                Expression.Return(returnStatement, pDefaultValue, pDefaultValue.Type),
                returnParsed);
            
            body = Expression.Block(pDefaultValue.Type, 
                new[] { value }, 
                stringValue, 
                returnValue, 
                Expression.Label(returnStatement, pDefaultValue));
        }
        
        return Expression.Lambda<Func<TSource, TValue, TValue>>(body, pElement, pDefaultValue);
    }
    
    private static class Cache<TValue>
    {
        public static readonly Func<XElement, TValue, TValue> ConvertElement 
            = BuildConverter<XElement, TValue>().Compile();
        
        public static readonly Func<XAttribute, TValue, TValue> ConvertAttribute
            = BuildConverter<XAttribute, TValue>().Compile();
    }
    
    public static T GetValue<T>(this XElement root, string name, T defaultValue)
    {
        return Cache<T>.ConvertElement(root.Element(name), defaultValue);
    }
    
    public static T GetAttribute<T>(this XElement root, string name, T defaultValue)
    {
        return Cache<T>.ConvertAttribute(root.Attribute(name), defaultValue);
    }
}

The meat of the code is the BuildConverter method. This uses the System.Linq.Expressions namespace to build a function that takes either an XElement or an XAttribute and a default value, and returns either the value of the element/attribute parsed to the specified type, or the default value if the element/attribute is null or cannot be parsed.

The method has three parts:

  1. Primitive types

    The non-nullable value types which have a corresponding explicit cast operator. The function will attempt to cast to the equivalent Nullable<> type, and coalesce to the default value.
    Equivalent to:

    C#
    (XElement element, double defaultValue) => (double?)element ?? defaultValue;
  2. Nullable types

    The Nullable<> value types which have a corresponding explicit cast operator. This group includes string, as the code would be identical.
    Equivalent to:

    C#
    (XElement element, double? defaultValue) => (double?)element ?? defaultValue;
  3. All other types

    The type must expose a public static method called TryParse, which accepts a string parameter and a by-reference parameter of the class type. The method is expected to return a bool indicating whether the parse was successful.
    Equivalent to:

    C#
    (XElement element, MyClass defaultValue) =>
    {
        string stringValue = (string)element;
        if (stringValue == null) return defaultValue;
        
        MyClass value;
        return MyClass.TryParse(stringValue, out value) ? value : defaultValue;
    }

The nested Cache<TValue> class is used to cache the converter methods for a specific return type. The methods will only be created as they are used, so there will be a performance hit when you first call the method for a specific return type. Subsequent calls for the same type should be significantly faster.

The GetValue and GetAttribute methods are the only public methods. They are called in exactly the same way as JSOP's original methods (July 2014 revision).

C#
int valueInt = element.GetAttribute("intAttribute", 0);
DateTime valueDate = element.GetValue("dateElement", DateTime.MinValue);

Points of Interest

The ability to define block lambda expressions via the expression tree API was added in .NET 4.0; if you're using .NET 3.5, you'll have to remove the third part of the method, and you'll be restricted to the 25 built-in types. In this case, it would probably be better to use the explicit cast operators directly.

Using a nested generic class as a strongly-typed cache might seem like a strange idea, but it seems to be faster than using a ConcurrentDictionary<Type, Delegate>, at least in my limited tests.

For most code, I would still be inclined to use the explicit cast operators directly. However, in cases where they don't work, this code might help. :)

History

  • 2014-07-18 - Initial version

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)