选择节点错误

时间:2014-01-17 17:31:24

标签: c# html xaml

我想在html

中得到这个
<div id="lyrics-body-text">
<p class='verse'>It's been a long and winding journey<br/>
But I'm finally here tonight picking up the pieces<br/>
And walking back into the night into the sunset of your glory<br/>
When my heart and future lies there's nothing like that feeling<br/>
When I look into your eyes</p></div>

使用C#XAML,我也使用HtmlAGilityPack 这是我的代码:

HttpClient wClient = new HttpClient();
responseData = await wClient.GetByteArrayAsync(URL);
UTF8Encoding utf8 = new UTF8Encoding();
String Lyrics = utf8.GetString(responseData, 0, responseData.Length);

StringBuilder pureText = new StringBuilder();
HtmlDocument doc = new HtmlDocument();
doc.LoadHtml(Lyrics);

try
{
     foreach (HtmlNode node in doc.DocumentNode.SelectNodes("//div[@id='lyrics-body-text']")) // error
     {
           pureText.Append(node.InnerText);
     }
}
catch
{ }

return pureText.ToString();

这是错误:

  

'HtmlAgilityPack.HtmlNode'不包含。的定义   'SelectNodes'并且没有扩展方法'SelectNodes'接受第一个   可以找到类型'HtmlAgilityPack.HtmlNode'的参数(是吗?   缺少using指令或程序集引用?)

1 个答案:

答案 0 :(得分:0)

如果没有看到其余的代码,就很难进行测试。不过,请尝试将DocumentNode.SelectNodes替换为DocumentElement.SelectNodes

从HtmlAgilityPack文档示例代码:

 HtmlDocument doc = new HtmlDocument();
 doc.Load("file.htm");
 foreach(HtmlNode link in doc.DocumentElement.SelectNodes("//a[@href"]))
 {
    HtmlAttribute att = link["href"];
    att.Value = FixLink(att);
 }
 doc.Save("file.htm");