解析形式HTML敏捷性包 [英] Parsing form with HTML Agility Pack
问题描述
我试图从一个表单中的所有输入元素。当我分析如下形式:
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屋!