Created
April 7, 2017 06:58
-
-
Save tluyben/64961ca5c78c6cea0dd887ebc7f3e968 to your computer and use it in GitHub Desktop.
Handy XML extensions
This file contains hidden or 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.Xml; | |
using System.Collections.Generic; | |
using System.Linq; | |
using System.Linq.Expressions; | |
namespace test1 | |
{ | |
public static class XMLExtensions | |
{ | |
public static T GetEnum<T>(this XmlAttributeCollection attributes, string key) { | |
if (!typeof(T).IsEnum) { | |
throw new ArgumentException ("Type must be an enum"); | |
} | |
key = attributes.Get (key); | |
return Enum.GetValues (typeof(T)).Cast<T> () | |
.Where (v => v.ToString ().IndexOf (key) >= 0) | |
.Select (v => v) | |
.First (); | |
} | |
public static string Get(this XmlAttributeCollection attributes, string key) { | |
if (attributes [key] != null) { | |
return attributes [key].Value; | |
} | |
return null; | |
} | |
} | |
} | |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment