循环在HTML表webbrower控件C# [英] Loop in HTML table webbrower control C#

查看:70
本文介绍了循环在HTML表webbrower控件C#的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

大家好吧

i有windows形式,其中有webbrower控件读取aweb页面这个页面有html表有id和有fur td每个都有atext和链接我怎么能得到每个td并将它们传递给td值上的文本框和一个值上的linkbutton我已经完成了这个简单的代码



  var  theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName(  table); 
foreach (HtmlElement elChild in theElementCollectionChildNext2)
{

if (elChild.Id == Table4
{
var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName( td);
foreach (HtmlElement elChild2 in theElementCollectionChildNext3)
{

// 我需要将每个td值传递给文本框

}
}





正确的语法是什么



我尝试过:



var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName(table);

foreach(theElementCollectionChildNext2中的HtmlElement elChild)

{



if(elChild.Id ==Table4)

{

var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName(td);

foreach(HtmlElement elC) hild2 inElementCollectionChildNext3)

{



//我需要将每个td值传递给文本框



}

}

解决方案

假设你的表结构是这样的

< pre lang =HTML> < table id = 表4 < span class =code-keyword>>
< tr >
< td > 1 < / td >
< td > 2 < / td >
< td > 3 < / td >
< td > 4 < / td >
< / tr >
< / table >



然后您必须按以下代码分配相应文本框的值,我建议你使用断点 [ ^ ]和debugger [ ^ ]使用对象属性进入循环,根据您可以自定义适合您需要的代码。



  private   void  webBrowser1_DocumentCompleted( object  sender,WebBrowserDocumentCompletedEventArgs e)
{
HtmlElement table = webBrowser1.Document.GetElementById( table4);
var rows = table.GetElementsByTagName( TR);
foreach (HtmlElement行 行)
{ int i = 0 ;
foreach (HtmlElement cell in row.GetElementsByTagName( td))
{
i ++;
string value = cell.InnerText;
if (i == 1
textBox1.Text = ;
if (i == 2
textBox2.Text = ;
if (i == 3
textBox3.Text = ;
if (i == 4
textBox4.Text = ;

}
}


}





由于你知道表的Id,你可以使用 GetElementById 而不是 TagName


我使用它并且工作正常



  var  theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName(  a); 
foreach (HtmlElement elChild in theElementCollectionChildNext2)
{

if (elChild.Id == HyperLink4
{
hyperlinkLabelControl1.Text = elChild.GetAttribute( href< /跨度>);
}

其他 如果(elChild.Id == < span class =code-string>
HyperLink5
{
hyperlinkLabelControl2.Text = elChild.GetAttribute( href);
}

其他 如果(elChild.Id == < span class =code-string> HyperLink6
{
hyperlinkLabelControl3.Text = elChild.GetAttribute( href);
}


hi everybody
i have windows form where there is webbrower control which read aweb page this page have html table which have id and there are fouur td each one have atext and link how can i get each td and pass them to textbox on td value and linkbutton on a value i have done this simple code

var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("table");
         foreach (HtmlElement elChild in theElementCollectionChildNext2)
         {

             if (elChild.Id == "Table4")
             {
                 var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName("td");
                 foreach (HtmlElement elChild2 in theElementCollectionChildNext3)
                 {

                     // i need to pass each td value to textbox

                 }
             }



what is the correct syntax to be done

What I have tried:

var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("table");
foreach (HtmlElement elChild in theElementCollectionChildNext2)
{

if (elChild.Id == "Table4")
{
var theElementCollectionChildNext3 = webBrowserwelcome.Document.GetElementsByTagName("td");
foreach (HtmlElement elChild2 in theElementCollectionChildNext3)
{

// i need to pass each td value to textbox

}
}

解决方案

Assume that your table structure is like this

<table id="Table4">
        <tr>
            <td>1</td>
            <td>2</td>
            <td>3</td>
            <td>4</td>
        </tr>
    </table>


then you will have to code as below to assign the values to the respective text box, i would recommend you to use break point[^] and debugger [^]to play around with the object and properties getting in the loop, based on that you can customize your code which suits your need.

private void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
       {
          HtmlElement table =  webBrowser1.Document.GetElementById("table4");
          var rows = table.GetElementsByTagName("tr");
          foreach (HtmlElement row in rows)
          {  int i = 0;
              foreach (HtmlElement cell in row.GetElementsByTagName("td"))
              {
                  i++;
                  string value = cell.InnerText;
                  if (i == 1)
                      textBox1.Text = value;
                  if (i == 2)
                      textBox2.Text = value;
                  if (i == 3)
                      textBox3.Text = value;
                  if (i == 4)
                      textBox4.Text = value;

              }
          }


       }



Since you knew the Id of the table, you can get the Table Element by using GetElementById instead of TagName


i use this and works fine

var theElementCollectionChildNext2 = webBrowserwelcome.Document.GetElementsByTagName("a");
                      foreach (HtmlElement elChild in theElementCollectionChildNext2)
                      {

                          if (elChild.Id == "HyperLink4")
                          {
                              hyperlinkLabelControl1.Text = elChild.GetAttribute("href");
                          }

                          else if (elChild.Id == "HyperLink5")
                          {
                              hyperlinkLabelControl2.Text = elChild.GetAttribute("href");
                          }

                          else if (elChild.Id == "HyperLink6")
                          {
                              hyperlinkLabelControl3.Text = elChild.GetAttribute("href");
                          }


这篇关于循环在HTML表webbrower控件C#的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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