Skip to content

Instantly share code, notes, and snippets.

@lavn0
Created January 18, 2017 11:36
Show Gist options
  • Save lavn0/38ebb8823d29c056d1c0b108e28e2a8b to your computer and use it in GitHub Desktop.
Save lavn0/38ebb8823d29c056d1c0b108e28e2a8b to your computer and use it in GitHub Desktop.
using System.Collections.Generic;
using System.Linq;
using System.Xml.Linq;
namespace System.Xml.XPath
{
public static class XPathUtility
{
public static IEnumerable<T> XPathSelectElements<T>(this XNode xNode, string xpath)
where T : XObject
{
return ((IEnumerable<object>)xNode.XPathEvaluate(xpath)).Cast<T>();
}
public static string GetValue(this XObject xObject)
{
return
xObject is XAttribute
? ((XAttribute)xObject).Value :
xObject is XText
? ((XText)xObject).Value
: ((XElement)xObject).Value;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment