Skip to content

Instantly share code, notes, and snippets.

@serialseb
Created November 17, 2011 14:34
Show Gist options
  • Save serialseb/1373274 to your computer and use it in GitHub Desktop.
Save serialseb/1373274 to your computer and use it in GitHub Desktop.
public static IEnumerable<Dictionary<string, string>> ItemScope(this XDocument document, string schema, params string[] propertyNames)
{
return from node in document.Document.Descendants()
where node.HAttr("itemtype") == schema
where node.HAttr("itemscope") == "itemscope"
select (from property in node.Descendants()
let propName = property.HAttr("itemprop")
where propertyNames.Contains(propName)
select property).ToDictionary(_=>_.HAttr("itemprop"), _=>_.Value);
}
public static string GetLinkValue(this WebResponse response, string rel)
{
return (from header in response.Headers["Link"].Split(',')
let components = header.Split(';')
let uri = components[0]
where components.Any(
component =>
Regex.IsMatch(component,
@"rel\s*=\s*" + Regex.Escape(rel)))
select uri.Substring(1, uri.Length - 2))
.FirstOrDefault();
}
const string XHTML_NS = "http://www.w3.org/1999/xhtml";
public static string HAttr(this XElement element, string attribName)
{
var attrib = element.Attribute(attribName);//XName.Get(attribName, XHTML_NS));
return attrib != null ? attrib.Value : null;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment