Parsing form with HTML Agility Pack

牧云@^-^@ 提交于 2019-12-08 11:54:08

问题


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>

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


回答1:


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.

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"];
}



回答2:


I don't remember what does ".Elements()" do, but I assume that it just returns the children nodes... and in your case the direct childs of your Form are divs.

You can use XPATH to have a little more control:

.SelectNodes("//form/div/input")

This will return a list of input nodes in a form, knowing that the input is withing a div tag.

Here you can see an XPATH TUTORIAL with examples.




回答3:


Use Descendants() instead of Elements() - the later only works on direct children but your input elements are nested within divs:

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


来源:https://stackoverflow.com/questions/9889404/parsing-form-with-html-agility-pack

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!