Skip to content

Instantly share code, notes, and snippets.

@tluyben
Created April 7, 2017 06:58
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tluyben/64961ca5c78c6cea0dd887ebc7f3e968 to your computer and use it in GitHub Desktop.
Save tluyben/64961ca5c78c6cea0dd887ebc7f3e968 to your computer and use it in GitHub Desktop.
Handy XML extensions
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