I need to parse form elements from a page that has two form tags one after another like so.
<divclass="new"><formaction="http://xxx.xxx.com/y"method="post"enctype="multipart/form-data"><inputtype="hidden"name="hf1"value="one"><inputtype="hidden"name="hf2"value="new"><inputtype="file"name="file"><buttontype="submit"name="do"value="new image">new image</button></form></div><divclass="done"><formaction="http://xxx.xxx.com/y/"method="post"><inputtype="hidden"name="hf1"value="one"><inputtype="hidden"name="hf2"value="done"><buttontype="submit"name="do"value="Done with Images">done</button></form>
To check it i used the following code.
HtmlAgilityPack.HtmlDocument doc = new HtmlAgilityPack.HtmlDocument(); HtmlNode.ElementsFlags.Remove("form"); doc.Load(new StringReader(streamData));var forms = doc.DocumentNode.Descendants("form");var count = 0;foreach (var form in forms) {foreach (HtmlNode element in form.SelectNodes("//input[@type='hidden']")) {if (!element.Attributes.Contains("name")) continue; System.Diagnostics.Debug.WriteLine("Form-"+ count.ToString() + " - Name: "+ element.Attributes["name"].Value + " - Value: "+ element.Attributes["value"].Value); } count++; }The issue is each form seems to have the hidden elements of both forms, the output is
Form-0 - Name: hf1 - Value: one
Form-0 - Name: hf2 - Value: add
Form-0 - Name: hf1 - Value: one
Form-0 - Name: hf2 - Value: done
Form-1 - Name: hf1 - Value: one
Form-1 - Name: hf2 - Value: add
Form-1 - Name: hf1 - Value: one
Form-1 - Name: hf2 - Value: doneWhat I was expecting was
Form-0 - Name: hf1 - Value: one
Form-0 - Name: hf2 - Value: add
Form-1 - Name: hf1 - Value: one
Form-1 - Name: hf2 - Value: done
I assume it has something to do with the two forms not having any name or id.
Any idea how fix this, FYI I do not have control over the page source.
Regards
Al