|
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Forms;
namespace NCrawler
{
public abstract class Selector
{
protected abstract IEnumerable<HtmlElement> SelectCore(HtmlDocument document);
public IEnumerable<HtmlElement> Select(HtmlDocument document)
{
var elements = SelectCore(document);
if(selectChildren != null)
return elements.SelectMany(e => e.Children.OfType<HtmlElement>()).Where(selectChildren);
return elements;
}
Func<HtmlElement, bool> selectChildren;
public Selector SelectChildren(Func<HtmlElement, bool> predicate)
{
selectChildren = predicate;
return this;
}
public void ForEach(HtmlDocument document, Action<HtmlElement> action)
{
foreach(var element in Select(document))
action(element);
}
public static implicit operator Selector(string id)
{
return new IdSelector(id);
}
}
public class IdSelector : Selector
{
public IdSelector(string id)
{
_Id = id;
}
private readonly string _Id;
public string Id
{
get
{
return _Id;
}
}
protected override IEnumerable<HtmlElement> SelectCore(HtmlDocument document)
{
yield return document.GetElementById(Id);
}
}
public class ClassSelector : Selector
{
public ClassSelector(string className)
{
_ClassName = className;
}
private readonly string _ClassName;
public string ClassName
{
get
{
return _ClassName;
}
}
protected override IEnumerable<HtmlElement> SelectCore(HtmlDocument document)
{
foreach(HtmlElement element in document.All)
{
dynamic dom = element.DomElement;
string className = dom.className;
if(className == ClassName)
yield return element;
}
}
}
}
|
By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.
If a file you wish to view isn't highlighted, and is a text file (not binary), please
let us know and we'll add colourisation support for it.
I am currently the CTO of
Metaco, we are leveraging the Bitcoin Blockchain for delivering financial services.
I also developed a tool to make IaaS on Azure more easy to use
IaaS Management Studio.
If you want to contact me, go
this way