Click here to Skip to main content
15,878,809 members
Articles / Programming Languages / C# 4.0
Alternative
Tip/Trick

Using Extension Methods To Avoid XML Problems

Rate me:
Please Sign up or sign in to vote.
3.40/5 (3 votes)
18 Jul 2014CPOL2 min read 8.7K   3   2
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)


Written By
Software Developer CodeProject
United Kingdom United Kingdom
I started writing code when I was 8, with my trusty ZX Spectrum and a subscription to "Input" magazine. Spent many a happy hour in the school's computer labs with the BBC Micros and our two DOS PCs.

After a brief detour into the world of Maths, I found my way back into programming during my degree via free copies of Delphi and Visual C++ given away with computing magazines.

I went straight from my degree into my first programming job, at Trinet Ltd. Eleven years later, the company merged to become ArcomIT. Three years after that, our project manager left to set up Nevalee Business Solutions, and took me with him. Since then, we've taken on four more members of staff, and more work than you can shake a stick at. Smile | :)

Between writing custom code to integrate with Visma Business, developing web portals to streamline operations for a large multi-national customer, and maintaining RedAtlas, our general aviation airport management system, there's certainly never a dull day in the office!

Outside of work, I enjoy real ale and decent books, and when I get the chance I "tinkle the ivories" on my Technics organ.

Comments and Discussions

 
GeneralThoughts Pin
PIEBALDconsult18-Jul-14 13:27
mvePIEBALDconsult18-Jul-14 13:27 
GeneralRe: Thoughts Pin
Richard Deeming21-Jul-14 1:08
mveRichard 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.