Get fields from a form in htmlagilitypack

html-agility-pack

Question

I want to get the data for a form so i wrote the below. It didnt work

doc.DocumentNode.SelectNodes("//form[@name='F1']//input[@name]");

Breaking it up into two steps did

var node = doc.DocumentNode.SelectSingleNode("//form[@name='F1']");
var nodes = node.SelectNodes("//input[@name]");

However i get the data from the entire html file rather then the node/form which is unexpected. How do i get the results from that form only? I tried /input[@name] and .//input[@name] which gave me null

Popular Answer

It seems this is default behavior for <form> tag parsing in Html Agility Pack. As they said here:

FORM is treated like this because many HTML pages used to have overlapping forms, as this was actually a (powerful) feature of original HTML. Now that XML and XHTML exist, everybody assumes that overlapping is an error, but it's not (in HTML 3.2).

You could change it by using:

HtmlNode.ElementsFlags.Remove("form");

and your "//form[@name='F1']//input[@name]" expression should work. Or change the second expression to ".//input[@name]" and it also should work:

var node = doc.DocumentNode.SelectSingleNode("//form[@name='F1']");
var nodes = node.SelectNodes(".//input[@name]");



Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why
Licensed under: CC-BY-SA with attribution
Not affiliated with Stack Overflow
Is this KB legal? Yes, learn why