Just a small bit of code, I've been playing around with converting some Xml* code to Linq to Xml code, and pretty successfully achieved everything I wanted, one thing missing was the ability to retrieve an XElement ignoring case.
First - I'd like mention that yes I know Xml is case sensitive - and this is a bad thing to do - but I needed to do it due to some wierdness in the input xml...
Anyhews, below is the extension method I wrote to deal with this..
public static class XElementExtensions
{
/// <summary>Gets the first (in document order) child element with the specified <see cref="XName"/>.</summary>
/// <param name="element">The element.</param>
/// <param name="name">The <see cref="XName"/> to match.</param>
/// <param name="ignoreCase">If set to <c>true</c> case will be ignored whilst searching for the <see cref="XElement"/>.</param>
/// <returns>A <see cref="XElement"/> that matches the specified <see cref="XName"/>, or null. </returns>
public static XElement Element( this XElement element, XName name, bool ignoreCase )
{
var el = element.Element( name );
if (el != null)
return el;
if (!ignoreCase)
return null;
var elements = element.Elements().Where( e => e.Name.LocalName.ToString().ToLowerInvariant() == name.ToString().ToLowerInvariant() );
return elements.Count() == 0 ? null : elements.First();
}
}
Cheers!