解析形式HTML敏捷性包 [英] Parsing form with HTML Agility Pack

查看:120
本文介绍了解析形式HTML敏捷性包的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个表单中的所有输入元素。当我分析如下形式:

I'm trying to extract all input elements from a form. When I parse the following form:

<form>
<input name='test1' type='text'>
<input name='test2' type='text'>
<input name='test3' type='text'>
</form>



一切都非常完美,HTML敏捷性包能够检测在表单中输入的内容,但如果它有。像下面这样,它不会被检测到一个div父节点

everything worked perfectly, HTML Agility Pack was able to detect the input elements in the form but if it has a div parent node like the following, it will not be detected.

<form>
<div><input name='test1' type='text'></div>
<div><input name='test2' type='text'></div>
<div><input name='test3' type='text'></div>
</form>



我用下面的代码

I'm using the following code

HtmlNode.ElementsFlags.Remove("form");

foreach (HtmlAgilityPack.HtmlNode node in postForm.Elements("input"))
{
    HtmlAgilityPack.HtmlAttribute valueAttribute = node.Attributes["value"];
}



谁能告诉我哪里出了问题?谢谢

Can anyone tell me what went wrong? Thanks

推荐答案

HtmlNode.Elements 方法获取匹配第一代子节点匹配名称。当你把你的投入在< DIV> 标签,他们成为了表单元素的第二代子节点

HtmlNode.Elements method gets matching first generation child nodes matching name. After you put your inputs inside a <div> tag they become the second generation child nodes for the form element.

为使你的代码工作中使用 HtmlNode.Descendants 方法,获得具有匹配名称的所有后代节点:

To make your code work use HtmlNode.Descendants method which gets all descendant nodes with matching name:

foreach (HtmlAgilityPack.HtmlNode node in postForm.Descendants("input"))
{
   HtmlAgilityPack.HtmlAttribute valueAttribute = node.Attributes["value"];
}

这篇关于解析形式HTML敏捷性包的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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