2010-11-19 118 views
0

HTML页面看起来像这样提取特定文本

<tr> 
<th rowspan="4" scope="row">General</th> 
<td class="ttl"><a href="network-bands.php3">2G Network</a></td> 
<td class="nfo">GSM 850/900/1800/1900 </td> 
</tr><tr> 
<td class="ttl"><a href="network-bands.php3">3G Network</a></td> 
<td class="nfo">HSDPA 900/1900/2100 </td> 
</tr> 

对此,我尝试使用

var text = document.getElementsByClassName("nfo")[0].innerHTML; 

提供由亚历克斯

但我得到这个错误 错误2名称'document'在当前上下文中不存在C:\ Users \ Nabi Javid \ Documents \ Visual Studio 2008 \ Projects \ WpfApplication2 \ WpfApplication2 \ Window1.xaml.cs 30 22 WpfApplication2

我失去了一些Libary什么

目前我的代码就是这样

using System; 
using System.Collections.Generic; 
using System.Linq; 
using System.Text; 
using System.Windows; 
using System.Windows.Controls; 
using System.Windows.Data; 
using System.Windows.Documents; 
using System.Windows.Input; 
using System.Windows.Media; 
using System.Windows.Media.Imaging; 
using System.Windows.Navigation; 
using System.Windows.Shapes; 

namespace WpfApplication1 
{ 
    /// <summary> 
    /// Interaction logic for Window1.xaml 
    /// </summary> 
    public partial class Window1 : Window 
    { 
     public Window1() 
     { 
      InitializeComponent(); 
     } 

     private void button1_Click(object sender, RoutedEventArgs e) 
     { 
      HtmlAgilityPack.HtmlDocument htmlDoc = new HtmlAgilityPack.HtmlDocument(); 
      htmlDoc.Load("nokia_c5_03-3578.html"); 
      var text = document.getElementsByClassName("nfo")[0].innerHTML; 

     } 
    } 

} 

回答

1

我还不是很深入的.NET,但它看起来像你正试图混合的JavaScript代码

var text = document.getElementsByClassName("nfo")[0].innerHTML; 

与您的.NET代码...?

+0

28:9他认为他的使用HAP,但他写的JavaScript。 – SLaks 2010-11-19 15:01:39

+0

@SLaks - 重读这个,你是对的。 – Oded 2010-11-19 15:02:03

+0

无论谁投降,请与我们分享您的知识。 – acme 2012-09-17 15:26:08

0

您必须使用htmlDoc变量来调用您的方法。 顺便说一句,HtmlDocument类没有这个名字的方法。尝试查看是否可以在this list中找到您需要的其他匹配项。

如错误所述,document变量不会退出您的代码。

0

你想

var text = htmlDoc.getElementsByClassName("nfo")[0].innerHTML; 

?不熟悉HTML敏捷包,但这似乎有意义

2

您正在混合C#代码与JavaScript代码。

取而代之的是:

var text = document.getElementsByClassName("nfo")[0].innerHTML; 

类型的:

var text = htmlDoc.DocumentNode.SelectNodes("//td[@class='nfo']")[0].InnerHtml; 

为了简单起见,我从检查的异常忍住了。

+0

谢谢clearfiy我以后如何将它保存在文本框? – 2010-11-19 15:19:17

+0

感谢问题已经解决了,再次感谢 – 2010-11-19 15:38:15

+0

这找不到有几个类的节点lile 。 – tbicr 2012-09-17 13:04:47

0

你可以使用下一个方法,通过类名称的元素它会返回,其中有一类属性定义的几类要素:

private HtmlNodeCollection GetElementsByClassName(HtmlDocument htmlDocument, string className) 
{ 
    string xpath = 
     String.Format(
      "//*[contains(concat(' ', normalize-space(@class), ' '), ' {0} ')]", 
      className); 
    return htmlDocument.DocumentNode.SelectNodes(xpath); 
}