无法从 HTML 文件获取 HTML 属性 (C#/WinForms)
我设法想出了以下代码:
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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
通过迭代
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?