HTMLAgilityPack Expression cannot contain lambda e

2019-01-29 09:29发布

问题:

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.