无法从 HTML 文件获取 HTML 属性 (C#/WinForms)

发布于 2024-10-21 20:27:11 字数 639 浏览 4 评论 0原文

我设法想出了以下代码:

private void button1_Click(object sender, EventArgs e)
{
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    using (var o = new OpenFileDialog())
    {
        if (o.ShowDialog() == DialogResult.OK)
            doc.Load(o.FileName);
    }


    foreach (HtmlAgilityPack.HtmlAttribute att in doc.DocumentNode.Attributes)
    {
        label1.Text += Environment.NewLine +
            att.Name + " " + att.Value;
    }
}

但它没有做任何事情。没有错误,没有异常,它可以编译并运行。但是,正如您所看到的,从 foreach 循环内部,它应该不断将找到的属性及其值添加到 label1.Text 控件,但事实并非如此。什么也没有发生!

我做错了什么吗?有人可以帮忙吗?

谢谢

I have the following code that I managed to come up with:

private void button1_Click(object sender, EventArgs e)
{
    HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument();
    using (var o = new OpenFileDialog())
    {
        if (o.ShowDialog() == DialogResult.OK)
            doc.Load(o.FileName);
    }


    foreach (HtmlAgilityPack.HtmlAttribute att in doc.DocumentNode.Attributes)
    {
        label1.Text += Environment.NewLine +
            att.Name + " " + att.Value;
    }
}

But it's not doing anything. There are no errors, no exceptions, and it compiles and runs. But, as you can see, from inside the foreach loop, it is supposed to keep adding found attributes and their values to the label1.Text control, but it isn't. Nothing happens!

Am I doing something wrong? Can someone please help?

Thank you

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

月亮邮递员 2024-10-28 20:27:11

通过迭代 doc.DocumentNode.Attributes,您尝试获取根元素 (DocumentNode) 的属性,该元素是包含 < 的占位符/code> 标签(可能还有一些相邻节点,例如注释和空格)。这没有多大意义。

您到底想提取什么?

By iterating over doc.DocumentNode.Attributes, you are trying to get attributes of the root element (DocumentNode) which is a placeholder containing your <html> tag (and possibly some adjacent nodes like comments and white space). Which does not make a lot of sense.

What are you trying to extract exactly?

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文