Getting data from an HTML table in C #

I want to get data from an HTML document. I am clearing data from a website that I almost did, but I get a problem when trying to retrieve data from a table. Here is the HTML code

<div id="middle_column">
<form action="url?" method="post" name="inquirydetail">
    <input type="hidden" name="ServiceName" value="SurgeWebService">
    <input type="hidden" name="TemplateName" value="Inpat_AvailableResponses.htm">
    <input type="hidden" name="CurrentPage" value="inquirydetail">
    <form method="post" action="url" name="ResponseSel" onSubmit="return EditPage(document.forms[3])">    
<TABLE
<tBody
 <table
....
</table

 <table
....
</table
 <table border="0" width="90%">
                    <tr>
                      <td width="10%" valign="bottom" class="content"> Service Number</td>
                      <td width="30%" valign="bottom" class="content"> Status</td>
                      <td width="50%" valign="bottom" class="content"> Status Date</td>
                    </tr>
                    <tr>
                      <td width="20%" bgcolor="white" class="subtitle">1</td>
                      <td width="40%" bgcolor="white" class="subtitle">Approved</td>
                      <td width="40%" bgcolor="white" class="subtitle">03042014</td>
                    </tr>
                    <tr>
                      <td></td>
                    </tr>
                  </table>
</tbody>
</TABle>
</div>

I need to get data for the Status field. Approved and written to SQL DB. There are many tables in the form tag. Tables do not have identifiers. How can I get the correct table, row and cell Here is my code

 HtmlElement tBody = WB.Document.GetElementById("middle_column");
   if (tBody != null)
                {
                   string sURL = WB.Url.ToString();
                    int iTableCount = tBody.GetElementsByTagName("table").Count;
                 }
   for (int i = 0; i <= iTableCount; i++)
                    {
                        HtmlElement tb=tBody.GetElementsByTagName("table")[i];
                    }

Something is wrong here. Please help with this.

+4
source share
2 answers

, Webbrowser? , id TD. .

, .

HtmlElementCollection tables = this.WB.Document.GetElementsByTagName("table");

            foreach (HtmlElement TBL in tables)
            {
                foreach (HtmlElement ROW in TBL.All)
                {

                    foreach (HtmlElement CELL in ROW.All)
                    {

                        // Now you are looping through all cells in each table

                        // Here you could use CELL.InnerText to search for "Status" or "Approved"
                    }
                }
            }

, , . .

, .

+2

DomElement, .net 4 +.

, . , , , .

, DOM, , . , , javascript, #.

HtmlElement myTableElement;
//Set myTableElement using any GetElement...  method.
//Use a loop or square bracket index if the method returns an HtmlElementCollection.
dynamic myTable = myTableElement.DomElement;
for (int i = 0; i < myTable.rows.length; i++)
{
    for (int j = 0; j < myTable.rows[i].cells.length; j++)
    {
        string CellContents = myTable.rows[i].cells[j].innerText;

        //You are not limited to innerText; you have the whole DOM available.

        //Do something with the CellContents.

    }
}
+1

Source: https://habr.com/ru/post/1535653/


All Articles