Reputation: 21
I wanted to import some data from HTML table to a datatable. From what I've read the best way to do it is to use HTML Agility. This is the code I used to do so but I'm getting error
'HtmlAgilityPack.HtmlNodeCollection' does not contain a definition for 'Select' and no extension method 'Select' accepting a first argument of type 'HtmlAgilityPack.HtmlNodeCollection' could be found (are you missing a using directive or an assembly reference?) C:\webbrowser\WebBrowserControlDialogs\MainForm.cs.
This is the error line :
otable.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
Any idea on how to fix this ? Appreciated
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
var document = webBrowser1.Document;
var documentAsIHtmlDocument3 = (mshtml.IHTMLDocument3)document.DomDocument;
var content = documentAsIHtmlDocument3.documentElement.outerHTML;
doc.LoadHtml(content);
DataTable otable = new DataTable();
otable.Columns.Add("ID .", typeof(string));
otable.Columns.Add("Art.", typeof(string));
otable.Columns.Add("E ram", typeof(int));
otable.Columns.Add("Hour", typeof(string));
otable.Columns.Add("S", typeof(int));
otable.Columns.Add("Ref", typeof(double));
foreach (var row in doc.DocumentNode.SelectNodes("//tr/td"))
{
otable.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
//...
Upvotes: 0
Views: 1957
Reputation: 56536
You're missing the using
that lets you use LINQ extension methods. Add this to the top of your file:
using System.Linq;
If you get the error Value cannot be null. Parameter name: source
when trying to call row.SelectNodes("td").Select(..)
, this means that row.SelectNodes("td")
is null
. You should correct your code and/or include appropriate null checking. I think that you want to select the tr
s at first, and then you can do SelectNodes("td")
to get the cells.
foreach (var row in doc.DocumentNode.SelectNodes("//tr"))
{
otable.Rows.Add(row.SelectNodes("td").Select(td => td.InnerText).ToArray());
}
Upvotes: 6