No child nodes for FORM objects

Topics: User Forum
Feb 23, 2011 at 12:09 PM

I've found that after parsing any <form/> node does not have any child nodes. All nodes that should be inside the form (<input/>, <select/>, etc.) are created as it's siblings rather then children.

Is it intended behavior (if it is why)? Or is it a bug?

Mar 2, 2011 at 8:07 AM

In Html specification form tag can overlap, so Htmlagilitypack handle this node a little different.  So what you can do:

HtmlNode.ElementsFlags.Remove("form");
Mar 2, 2011 at 8:47 AM

It helped. After adding this call all form elements are added as children. Many thanks for the tip.

Mar 11, 2011 at 5:33 PM

Hello,

 

I have the same problem but after adding "HtmlNode.ElementsFlags.Remove("form");" child nodes are empty, I can select childs by using selectnodes(".//input")

I have 2 forms on page

Can anyone help? 

var forms = doc.DocumentNode.Descendants("form");
foreach (var form in forms)
{
//this works correctly
foreach (HtmlNode element in form.SelectNodes(".//input"))
{
if (!element.Attributes.Contains("name")) continue;
Console.WriteLine("Name-input: " + element.Attributes["name"].Value);
}

//this not work
var inputs = form.ChildNodes
.Where<HtmlNode>(a => a.OriginalName.Contains("input"));

Console.WriteLine(form.Attributes[0].Value + " attributes:" + Environment.NewLine + "------------------");
foreach (var input in inputs)
{
IEnumerable<HtmlAttribute> attributes;
attributes = input.Attributes;
foreach (var att in attributes)
{
Console.WriteLine("Name: " + att.Name + Environment.NewLine
+ "Value: " + att.Value + Environment.NewLine);
}
}
Console.WriteLine();
}

Mar 11, 2011 at 5:46 PM

2 mrjozo

Are you sure that inputs are direct descendants of the form? If your input elements are in divs or table then your search in ChildNodes will yield empty set.

Mar 11, 2011 at 9:08 PM

Ariman You are right, there are in divs

Thanks

Feb 23, 2012 at 7:23 PM
VikciaR wrote:

In Html specification form tag can overlap, so Htmlagilitypack handle this node a little different.  So what you can do:

HtmlNode.ElementsFlags.Remove("form");

Thanks this worked!

Sep 12, 2012 at 1:19 PM

i am also facing same problem even "HtmlNode.ElementsFlags.Remove("form");" did not help me , all i am getting is just form starting tag nothing else , 

input fields are directly under the form tag

childnode count is 0.

please help me to find the correct way of doing this 

please help as soon as possible....

Jul 15, 2013 at 1:10 AM
In Html specification form tag can overlap, so Htmlagilitypack handle this node a little different.
Can you clarify what you mean here? This is pretty confusing default behavior.