2017-03-27 2 views
0

Im Start Hexe C# und ich versuchte, einen Tisch von einer Website zu bekommen und zeigen Sie es in einem Datagridview. Ich habe versucht, die ersten zwei Spalten zuerst zu bekommen, und ich kann die Daten bekommen, aber es zeigt in der zweiten Spalte.get html Tabelle in C# mit htmlagilitypack

using System; 
using System.Collections.Generic; 
using System.ComponentModel; 
using System.Data; 
using System.Drawing; 
using System.Linq; 
using System.Text; 
using System.Windows.Forms; 
using System.Web; 
using System.Net; 
using HtmlAgilityPack; 

namespace WindowsFormsApplication6 
{ 
    public partial class Form1 : Form 
    { 
     private DataTable dt; 
     public Form1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, EventArgs e) 
     { 
      string htmlCode = ""; 
      using (WebClient client = new WebClient()) 
      { 
       client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError"); 
       htmlCode = client.DownloadString("http://www.sismologia.cl/links/ultimos_sismos.html"); 
      } 
      HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 

      doc.LoadHtml(htmlCode); 

      dt = new DataTable(); 
      dt.Columns.Add("Local", typeof(string)); 
      dt.Columns.Add("UTC", typeof(string)); 

      int count = 0; 

      foreach (HtmlNode table in doc.DocumentNode.SelectNodes("//table")) 
      { 
       richTextBox1.Text = htmlCode; 

       foreach (HtmlNode row in table.SelectNodes("//*[@id=\"main\"]/table//tr")) 
       { 
        foreach (var cell in row.SelectNodes("//*[@id=\"main\"]/table//tr//td")) 
        { 
         DataRow dr = dt.NewRow(); 
         if ((count % 2 == 0)) 
         { 
          dr["Local"] = cell.InnerText.Replace(" ", " "); 
         } 
         else 
         { 
          dr["UTC"] = cell.InnerText.Replace(" ", " "); 

          dt.Rows.Add(dr); 
         } 
         count++; 
        } 
       } 

       dataGridView1.DataSource = dt; 
      } 
     } 
    } 
} 

Antwort

0

Die Lösung, wählen Sie direkt alle tr in main div und Schleife durch:

 string htmlCode = ""; 
     using (WebClient client = new WebClient()) 
     { 
      client.Headers.Add(HttpRequestHeader.UserAgent, "AvoidError"); 
      htmlCode = client.DownloadString("http://www.sismologia.cl/links/ultimos_sismos.html"); 
     } 
     HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); 

     doc.LoadHtml(htmlCode); 

     var dt = new DataTable(); 
     dt.Columns.Add("Local", typeof(string)); 
     dt.Columns.Add("UTC", typeof(string)); 

     foreach (var row in doc.DocumentNode.SelectNodes("//*[@id='main']//tr")) 
     { 
      var nodes = row.SelectNodes("td"); 
      if (nodes != null) 
      { 

       var local = nodes[0].InnerText; 
       var utc = nodes[1].InnerText; 

       dt.Rows.Add(local, utc); 
      } 
     } 
Verwandte Themen