public static class ExtensionMethods { public static string SafeAttribute(this XElement element, string key) { var attribute = element.Attribute(key); return attribute != null ? element.Attribute(key).Value : ""; } public static T SafeAttribute<T>(this XElement element, string key) { var attribute = SafeAttribute(element, key); return !string.IsNullOrEmpty(attribute) ? (T)Enum.Parse(typeof(T), attribute) : default(T); } }
Any this is how you would call it:
var xml = XDocument.Load(filePath); var e = xml.Element("Item").SafeAttribute<MyEnum>("myKey");
No comments:
Post a Comment