Created
June 4, 2014 08:58
-
-
Save Seikilos/a4bf12d2085f0d2a68d1 to your computer and use it in GitHub Desktop.
XElement extension for proper retrieval of elements and attributes including better exception handling
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
using System; | |
using System.Globalization; | |
using System.Xml.Linq; | |
namespace YourNS | |
{ | |
public static class Utils | |
{ | |
#region Attribute Helper | |
/// <exception cref="ArgumentException"></exception> | |
public static string GetAttributeOrThrow(this XElement element, string attribute) | |
{ | |
var attr = element.Attribute(attribute); | |
if (attr == null) | |
{ | |
throw new ArgumentException(string.Format("Could not read attribute '{0}' from element {1}", attribute, element)); | |
} | |
return attr.Value; | |
} | |
public static T GetAttributeOrDefault<T>(this XElement e, string attr, T def = default(T)) | |
{ | |
var a = e.Attribute(attr); | |
if (a == null) | |
{ | |
return def; | |
} | |
return (T) Convert.ChangeType(a.Value, typeof (T), CultureInfo.InvariantCulture); | |
} | |
#endregion | |
#region Element Helper | |
/// <exception cref="ArgumentException"></exception> | |
public static string GetElementOrThrow(this XElement element, string elementName) | |
{ | |
if (element == null) | |
{ | |
throw new ArgumentException("Passed element was null"); | |
} | |
var newE = element.Element(elementName); | |
if (newE == null) | |
{ | |
throw new ArgumentException(string.Format("Could not get element '{0}' from element {1}", elementName, element)); | |
} | |
return newE.Value; | |
} | |
/// <exception cref="ArgumentException"></exception> | |
public static T GetElementConvertedOrThrow<T>(this XElement element, string elementName) | |
{ | |
var newE = element.Element(elementName); | |
if (newE == null) | |
{ | |
throw new ArgumentException(string.Format("Could not get element '{0}' from element {1}", elementName, element)); | |
} | |
try | |
{ | |
return (T)Convert.ChangeType(newE.Value, typeof(T), CultureInfo.InvariantCulture); | |
} | |
catch (Exception e) | |
{ | |
throw new ArgumentException(string.Format("Could not get convert value '{0}' from element name {1} to type {2} from element {3}. Resulting exception: {4}", newE, elementName, typeof(T), element, e)); | |
} | |
} | |
public static T GetElementOrDefaultIfNotExist<T>(this XElement e, string elementName, T def = default(T)) | |
{ | |
var a = e.Element(elementName); | |
if (a == null) | |
{ | |
return def; | |
} | |
var underlying = Nullable.GetUnderlyingType(typeof(T)); | |
// Handle nullables properly | |
if (underlying != typeof(T)) | |
{ | |
// This is a nullable property | |
return (T)Convert.ChangeType(a.Value, underlying, CultureInfo.InvariantCulture); | |
} | |
return (T) Convert.ChangeType(a.Value, typeof (T), CultureInfo.InvariantCulture); | |
} | |
#endregion | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment