HTMLAgilityPack 表达式不能包含 lambda 表达式 [英] HTMLAgilityPack Expression cannot contain lambda expressions

查看:44
本文介绍了HTMLAgilityPack 表达式不能包含 lambda 表达式的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想要名为专辑笔记的 div 的 InnerText.正如我在许多其他地方所做的那样,我的代码如下:

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;

TextBlock,lblNotes,最终没有文本作为结果.如果我在调试模式下打开 QuickWatch,我会得到以下结果:

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:

表达式不能包含 lambda 表达式

Expression cannot contain lambda expressions

即使我在同一个应用程序的其他地方至少使用了 10 次完全相同的语法.奇怪的是它实际上并没有抛出错误或任何东西,它只是用一个空字符串填充 TextBlock.

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.

我的代码有什么问题?

推荐答案

表达式不能包含 lambda 表达式 消息不是来自 HTMLAgilityPack,而是来自 QuickWatch 功能.基本上,一个 lambda 表达式只是一个语法糖:在编译时,lambda 被转换为一个真正的"函数.由于它是在编译期间发生的事情,因此您无法在运行时(即在 QuickWatch 窗口中)创建全新的 lambda.

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).

现在的问题是:为什么 lblNotes.Text 是空的?不幸的是,如果没有看到 HTML 代码,我无法知道.但是,如果没有错误,则意味着已找到album_notes"div(否则您将遇到空引用异常).因此,InnerHtml 属性可能为空.

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;
}

这样,如果在最后一行设置断点,就可以在quickwatch窗口查看divdiv.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.

这篇关于HTMLAgilityPack 表达式不能包含 lambda 表达式的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆