如何使用 HtmlAgilityPack 获取表单中的所有输入元素而不出现空引用错误
HTML 示例:
<html><body>
<form id="form1">
<input name="foo1" value="bar1" />
<!-- Other elements -->
</form>
<form id="form2">
<input name="foo2" value="bar2" />
<!-- Other elements -->
</form>
</body></html>
测试代码:
HtmlDocument doc = new HtmlDocument();
doc.Load(@"D:\test.html");
foreach (HtmlNode node in doc.GetElementbyId("form2").SelectNodes(".//input"))
{
Console.WriteLine(node.Attributes["value"].Value);
}
语句 doc.GetElementbyId("form2").SelectNodes(".//input")
为我提供了一个空引用。
我有做错什么吗?谢谢。
Example HTML:
<html><body>
<form id="form1">
<input name="foo1" value="bar1" />
<!-- Other elements -->
</form>
<form id="form2">
<input name="foo2" value="bar2" />
<!-- Other elements -->
</form>
</body></html>
Test code:
HtmlDocument doc = new HtmlDocument();
doc.Load(@"D:\test.html");
foreach (HtmlNode node in doc.GetElementbyId("form2").SelectNodes(".//input"))
{
Console.WriteLine(node.Attributes["value"].Value);
}
The statement doc.GetElementbyId("form2").SelectNodes(".//input")
gives me a null reference.
Anything I did wrong? thanks.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您可以执行以下操作:
默认情况下,HTML Agility Pack 将表单解析为空节点,因为允许它们与其他 HTML 元素重叠。第一行 (
HtmlNode.ElementsFlags.Remove("form");
) 禁用此行为,允许您获取第二个表单内的输入元素。更新:
表单元素重叠的示例:
元素在表格内部开始,但在表格元素外部关闭。 HTML 规范允许这种情况,HTML Agility Pack 必须处理它。
You can do the following:
By default HTML Agility Pack parses forms as empty node because they are allowed to overlap other HTML elements. The first line, (
HtmlNode.ElementsFlags.Remove("form");
) disables this behavior allowing you to get the input elements inside the second form.Update:
Example of form elements overlap:
The element begins inside a table but is closed outside the table element. This is allowed in the HTML specification and HTML Agility Pack has to deal with it.
只需将它们放入数组即可:
Just get them in array: