Note that there are some explanatory texts on larger screens.

plurals
  1. POGetting data from HTML table into a datatable
    primarykey
    data
    text
    <p>Ok so I need to query a live website to get data from a table, put this HTML table into a DataTable and then use this data. I have so far managed to use Html Agility Pack and XPath to get to each row in the table I need but I know there must be a way to parse it into a DataTable. (C#) The code I am currently using is:</p> <pre><code>string htmlCode = ""; using (WebClient client = new WebClient()) { htmlCode = client.DownloadString("http://www.website.com"); } HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); doc.LoadHtml(htmlCode); //My attempt at LINQ to solve the issue (not sure where to go from here) var myTable = doc.DocumentNode .Descendants("table") .Where(t =&gt;t.Attributes["summary"].Value == "Table One") .FirstOrDefault(); //Finds all the odd rows (which are the ones I actually need but would prefer a //DataTable containing all the rows! foreach (HtmlNode cell in doc.DocumentNode.SelectNodes("//tr[@class='odd']/td")) { string test = cell.InnerText; //Have not gone further than this yet! } </code></pre> <p>The HTML table on the website I am querying looks like this: </p> <pre><code>&lt;table summary="Table One"&gt; &lt;tbody&gt; &lt;tr class="odd"&gt; &lt;td&gt;Some Text&lt;/td&gt; &lt;td&gt;Some Value&lt;/td&gt; &lt;/tr&gt; &lt;tr class="even"&gt; &lt;td&gt;Some Text1&lt;/td&gt; &lt;td&gt;Some Value1&lt;/td&gt; &lt;/tr&gt; &lt;tr class="odd"&gt; &lt;td&gt;Some Text2&lt;/td&gt; &lt;td&gt;Some Value2&lt;/td&gt; &lt;/tr&gt; &lt;tr class="even"&gt; &lt;td&gt;Some Text3&lt;/td&gt; &lt;td&gt;Some Value3&lt;/td&gt; &lt;/tr&gt; &lt;tr class="odd"&gt; &lt;td&gt;Some Text4&lt;/td&gt; &lt;td&gt;Some Value4&lt;/td&gt; &lt;/tr&gt; &lt;/tbody&gt; &lt;/table&gt; </code></pre> <p>I'm not sure whether it is better/easier to use LINQ + HAP or XPath + HAP to get the desired result, I tried both with limited success as you can probably see. This is the first time I have ever made a program to query a website or even interact with a website in any way so I am very unsure at the moment! Thanks for any help in advance :)</p>
    singulars
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload