Click here to Skip to main content
12,884,793 members (28,165 online)
Click here to Skip to main content
Add your own
alternative version

Tagged as


3 bookmarked
Posted 18 Jul 2014

Using Extension Methods To Avoid XML Problems

, 18 Jul 2014 CPOL
Rate this:
Please Sign up or sign in to vote.
This is an alternative for "Using Extension Methods To Avoid XML Problems"


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.


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. :)

public static class XmlExtensions
    private static readonly Type[] PrimitiveTypes =
        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);
            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(
                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),
            body = Expression.Block(pDefaultValue.Type, 
                new[] { value }, 
                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:

    (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:

    (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:

    (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).

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. :)


  • 2014-07-18 - Initial version


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


About the Author

Richard Deeming
Software Developer Nevalee Business Solutions
United Kingdom United Kingdom
No Biography provided

You may also be interested in...


Comments and Discussions

GeneralThoughts Pin
PIEBALDconsult18-Jul-14 13:27
memberPIEBALDconsult18-Jul-14 13:27 
GeneralRe: Thoughts Pin
Richard Deeming21-Jul-14 1:08
professionalRichard Deeming21-Jul-14 1:08 

General General    News News    Suggestion Suggestion    Question Question    Bug Bug    Answer Answer    Joke Joke    Praise Praise    Rant Rant    Admin Admin   

Use Ctrl+Left/Right to switch messages, Ctrl+Up/Down to switch threads, Ctrl+Shift+Left/Right to switch pages.

Permalink | Advertise | Privacy | Terms of Use | Mobile
Web02 | 2.8.170424.1 | Last Updated 18 Jul 2014
Article Copyright 2014 by Richard Deeming
Everything else Copyright © CodeProject, 1999-2017
Layout: fixed | fluid