user34537
user34537

Reputation:

Get fields from a form in htmlagilitypack

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

Upvotes: 0

Views: 2101

Answers (1)

Oleks
Oleks

Reputation: 32343

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

Upvotes: 3

Related Questions