2015-09-19 15 views
0

我试图下载一个网页并尝试在Windows通用应用程序中提取一些特定数据。我正在使用HtmlAgilityPack我写了下面的代码段。但我有一些错误。'HtmlDocument'不包含'GetElementById'的定义

async public void LoadDataFromWeb() 
    { 
     var client = new HttpClient(); // Add: using System.Net.Http; 
     var url = @"http://www.dsebd.org/displayCompany.php?name=NBL"; 
     string sourcePage = await client.GetStringAsync(url); 
     var doc = new HtmlDocument(); 
     doc.LoadHtml(sourcePage); 
     HtmlNode specificNode = doc.GetElementById("nodeId"); 
     HtmlNodeCollection nodesMatchingXPath = specificNode.DocumentNode.SelectNodes("x/path/nodes"); 
    } 

错误:

  1. “的HTMLDocument”不包含“的getElementById”的定义,并没有扩展方法“的getElementById”接受式“的HTMLDocument”的第一个参数可以找到(是否缺少)使用指令或程序集引用?)
  2. 'HtmlNode'不包含'DocumentNode'的定义,并且没有找到接受类型'HtmlNode'的第一个参数的扩展方法'DocumentNode'(您是否缺少using指令或装配参考?)

可能是什么问题?

+0

你把这个'使用HtmlAgilityPack; '在你的cs文件的顶部,并且是将库添加到项目的引用中? – Sybren

+0

是的......我确实把... @Sybren – Leon

+1

看来你正在使用[Browser.HtmlDocument]的方法(https://msdn.microsoft.com/en-us/library/system.windows.browser.htmldocument %28v = vs.95%29.aspx?f = 255&MSPPError = -2147217396)或[Forms.HtmlDocument](https://msdn.microsoft.com/en-us/library/system.windows.forms.htmldocument%28v = vs.110%29.aspx)。看看http://www.mikesdotnetting.com/article/273/using-the-htmlagilitypack-to-parse-html-in-asp-net关于如何刮的方法。确保你有明确的方式来引用HtmlAgilityPack的HtmlDocument – shahkalpesh

回答

0

'HtmlDocument' does not contain a definition for 'GetElementById' and no extension method 'GetElementById' accepting a first argument of type 'HtmlDocument' could be found (are you missing a using directive or an assembly reference?)

你应该使用 doc.GetElement b YID(),而不是doc.GetElement YID()

'HtmlNode' does not contain a definition for 'DocumentNode' and no extension method 'DocumentNode' accepting a first argument of type 'HtmlNode' could be found (are you missing a using directive or an assembly reference?)

我认为它可以通过

 specificNode.OwnerDocument.DocumentNode 
被替换

因为它们都返回HtmlNodeCollection

不知道这个虽然

相关问题