How to get form input with HtmlAgilityPack without getting a null reference error?

c# html html-agility-pack

Question

How can I extract the second set of input nodes after <br> from the form HTML below?

When I try the following, I get no input nodes.

HtmlNode.ElementsFlags.Remove("form");
HtmlNode formNode = doc.DocumentNode.SelectNodes("//form")[0];
foreach (HtmlNode innode in formNode.Elements("input"))
{
 ...
}

HTML

<form>
  <div>
    <input type="submit" value="subv1" name="subn1">
  </div>
  <script></script>
  <table></table>
  <table></table>
  <br>
  <input type="hidden" value="v1" name="n1">
  <input type="hidden" value="v2" name="n2">
  <input type="submit" value="subv2" name="subn2">
</form>  

Accepted Answer

Call HtmlNode.ElementsFlags.Remove("form") before you load the document. The following works fine:

public static void Main()
{
    HtmlNode.ElementsFlags.Remove("form");

    var doc = new HtmlDocument();
    doc.Load("HtmlPage1.html");

    HtmlNode formNode = doc.DocumentNode.SelectNodes("//form")[0];
    foreach (HtmlNode innode in formNode.Elements("input"))
    {
        Console.WriteLine(innode.OuterHtml);
    }

    Console.WriteLine("Press Enter to exit...");
    Console.ReadLine();
}



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