检索C#从HTML表中的数据 [英] Retrieve data from HTML table in C#

查看:105
本文介绍了检索C#从HTML表中的数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想检索HTML文档中的数据。
我是从一个网站,我几乎做了尝试从表中检索数据时,数据拼抢,但得到的问题。
这里是HTML代码

I want to retrieve data from HTML document. I am scraping data from a web site I almost done but get issue when tried to retrieve data from the table. Here is 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>



我在SQL数据库
找回它被批准用于状态字段数据,并把它写有许多表格的形式tag.Tables没有IDs.How我能得到正确的表,行和单元格
这里是我的代码

I have to retrieve data for Status field It is Approved and write it in SQL DB There are many tables in the form tag.Tables do not have IDs.How I can get 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];
                    }



有些事情错在这里
请this.Thank您有所帮助。

Something is wrong here Please help with this.Thank you.

推荐答案

你没有在页面的任何控制WebBrowser控件中显示?如果你这样做,最好添加一个ID字段状态TD。然后,你的生活会容易得多。

Don't you have any control over the page being displayed within the Webbrowser control? If you do it's better you add an id field for status TD. Then your life would be much easier.

总之,这里是你如何可以搜索一个表中的值。

Anyway, here's how you could search a value within a table.

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"
                    }
                }
            }

但是,这不是一个好方法,因为你是通过循环每个表和每个表内的每一个细胞找到你的文字。保持这个作为最后的选择。

But, this is not a good approach as you are looping through each table and each cell within each table to find your text. Keep this as the last option.

希望这可以帮助你得到一个想法。

Hope this helps you to get an idea.

这篇关于检索C#从HTML表中的数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆