public static IEnumerable <T> Links <T>(this ParsedHtml self, Func <string, HtmlObject, T> selector) { return (from a in self.QuerySelectorAll("a[href]") let href = a.GetAttributeValue("href") where !string.IsNullOrWhiteSpace(href) select selector(self.TryBaseHref(href), a)); }
public static IQuery <DataTable> FormsAsDataTable(ParsedHtml html) { var forms = from f in html.Forms select f.GetForm((fd, fs) => new { f.Name, Action = new Uri(html.TryBaseHref(f.Action), UriKind.Absolute), Method = f.Method.ToString().ToUpperInvariant(), f.EncType, Data = fd, Submittables = fs, }); var dt = new DataTable(); dt.Columns.AddRange(new [] { new DataColumn("#") { AllowDBNull = false }, new DataColumn("FormName") { AllowDBNull = true }, new DataColumn("FormAction") { AllowDBNull = false }, new DataColumn("FormMethod") { AllowDBNull = true }, new DataColumn("FormEncoding") { AllowDBNull = true }, new DataColumn("Name") { AllowDBNull = false }, new DataColumn("Value") { AllowDBNull = false }, new DataColumn("Submittable", typeof(bool)) { AllowDBNull = false }, }); foreach (var form in from fi in forms.Select((f, i) => (i + 1).AsKeyTo(f)) let form = fi.Value from controls in new[] { from e in form.Data.AsEnumerable() from v in e.Value select new { e.Key, Value = v, Submittable = false }, from e in form.Submittables.AsEnumerable() from v in e.Value select new { e.Key, Value = v, Submittable = true }, } from control in controls select new object[] { fi.Key, form.Name, form.Action.OriginalString, form.Method, form.EncType, control.Key, control.Value, control.Submittable, }) { dt.Rows.Add(form); } return(Query.Singleton(dt)); }