Quantcast
Channel: htmlagilitypack Forum Rss Feed
Viewing all articles
Browse latest Browse all 655

New Post: Parse inner HTML tables to a DataTable issue from a web page

$
0
0
Hi All,
     I need to parse inner HTML tables from a web page and I am using HTML Agility pack for it. Here is my HTML table and C# code.
Here is the issue. I have 3 Inner HTML tables.

1st Inner table - 3 rd level
2 nd HTML table - 3 rd level
3 rd HTML table - 2 nd Level

When looping through the Main HTML table, it has to stop at the third level and has to populate the DataTable as there is the first inner table. But instead it's going to the next level and populating the table.

Same thing with the 2 nd table.

For 3 HTML table, it has to come out the 3 rd loop and has to populate the DataTable.

Can someone guide me, where exactly i ma doing mistake in my code and what is the error.
<table> <tr><td><table><tr><td><table><tr><td><b>Daily Backup Failed Client Report - Corporate</b></font></td></tr></table></td><td><table><tr><td>Last Day: 8/28/14 09:06 - 8/29/14 09:06</td></tr><tr><td>NetBackup Master and Media Servers</td></tr></table></td></tr></table></td></tr> <tr><td><!-------------------- the actual table ------------------------><table><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>1</td><td>2</td><td>3</td></tr><tr><td>1</td><td>2</td><td>3</td></tr></table></td></tr><tr><td><br><hr>Generated by Data Protection Advisor v6.1.0 (Build 85670)<br>Date: 8/29/14 09:08</td></tr></table>
                foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table"))
                {
                    ///This is the table.
                    foreach (HtmlNode row in table.SelectNodes("tr").Skip(1))
                    {
                        ///This is the row.
                       foreach (HtmlNode cell in row.SelectNodes("td"))
                         ///can also use "th|td", but right now we ONLY need td
                         {
                             //This is the cell.
                            if (cell.InnerHtml.Contains("table"))
                             {
                                 foreach (HtmlNode subtable in cell.SelectNodes("//table"))
                                 {
                                     foreach (HtmlNode subrow in subtable.SelectNodes("tr").Skip(1))
                                     {
                                         foreach (HtmlNode subcell in subrow.SelectNodes("td"))
                                         {
                                             if (subcell.InnerHtml.Contains("table"))
                                             {
                                                 foreach (HtmlNode subsubtable in subcell.SelectNodes("//table"))
                                                 {
                                                     foreach (HtmlNode subsubrow in subsubtable.SelectNodes("tr").Skip(1))
                                                     {
                                                         foreach (HtmlNode subsubcell in subsubrow.SelectNodes("td"))
                                                         {
                                                             if (subsubcell.InnerHtml.Contains("table"))
                                                             {
                                                                 foreach (HtmlNode subsubsubtable in subsubcell.SelectNodes("//table"))
                                                                 {
                                                                     foreach (HtmlNode subsubsubrow in subsubsubtable.SelectNodes("tr").Skip(1))
                                                                     {
                                                                         foreach (HtmlNode subsubsubcell in subsubsubrow.SelectNodes("td"))
                                                                         {                                                                              
                                                                             dccc.Columns.Add(subsubcell.InnerText);
                                                                             dataGridView1.DataSource = dccc;
                                                                         }

                                                                     }
                                                                 }
                                                             }
                                                             else
                                                             {
                                                                 if (dm1.Rows.Count == 0)
                                                                 {
                                                                     dm1.Columns.Add(subsubcell.InnerText);
                                                                     dataGridView1.DataSource = dm1;
                                                                 }
                                                                 else 
                                                                 {
                                                                     dm2.Columns.Add(subsubcell.InnerText);
                                                                     dataGridView1.DataSource = dm2;                                                                     
                                                                 }                                       
                                                             }
                                                         }                                                           
                                                     }
                                                     dc.Columns.Add(subcell.InnerText);
                                                     dataGridView2.DataSource = dc;
                                                 }
                                             }
                                             else
                                             {
                                                 dm1.Columns.Add(subcell.InnerText);
                                                 dataGridView1.DataSource = dm1;                                                    
                                             }
                                         }
                                     }
                                 }
                             }
                             else
                             {                                     
                                 dmm.Columns.Add(cell.InnerText);
                             }
                         }
                     }
                 }

Viewing all articles
Browse latest Browse all 655

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>