HTMLAgilityPack Expression cannot contain lambda expressions

人盡茶涼 提交于 2019-11-28 12:42:41

问题


I want the InnerText of the div called album_notes. As I did in many other places, my code is the following:

public void Album_Notes(HtmlAgilityPack.HtmlDocument bandHTML)
{
        this.lblNotes.Text = bandHTML.DocumentNode.Descendants("div").First(x => x.Id == "album_notes").InnerHtml;

The TextBlock, lblNotes, ends up with no text as the result. If I open the QuickWatch while in debug mode, I get the following result:

Expression cannot contain lambda expressions

even though I've used the exact same syntax at least 10 other times elsewhere in the same app. The odd thing is that it doesn't actually throw an error or anything, it just fills the TextBlock with an empty string.

What's wrong with my code?


回答1:


The Expression cannot contain lambda expressions message doesn't come from the HTMLAgilityPack but from the QuickWatch feature. Basically, a lambda expression is just a syntaxic sugar: upon compilation the lambda is converted to a 'real' function. Since it's something happening during compilation, you can't create a brand new lambda at runtime (that is, in the QuickWatch window).

Now the question is: why is lblNotes.Text empty? Unfortunately, I can't know without seeing the HTML code. Though, if there's no error, it means that the "album_notes" div has been found (otherwise you would have a null reference exception). Therefore, the InnerHtml property is probably empty.

You can check that by rewriting your code a bit:

public void Album_Notes(HtmlAgilityPack.HtmlDocument bandHTML)
{
    var div = bandHTML.DocumentNode.Descendants("div").First(x => x.Id == "album_notes");
    this.lblNotes.Text = div.InnerHtml;
}

This way, if you put a breakpoint on the last line, you can check the value of div and div.InnerHtml in the quickwatch window.



来源:https://stackoverflow.com/questions/10469408/htmlagilitypack-expression-cannot-contain-lambda-expressions

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!