Bookmark and Share BitCoin Donate: 13t8gAWVpHP2ddxMp88d1NFpZjnFJC6UwK

Get form elements from HTML document in dotnet

using HtmlAgilityPack;
using System.Collections.Generic;
using System.Linq;
using System;

namespace ashrdotnet
{
    public class HtmlAgilityPackTest
    {
        public void getFormElementsFromHTMLDocument(string HTML)
        {
            Dictionary fields = new Dictionary();
            HtmlDocument htmlDoc = new HtmlDocument();
            htmlDoc.LoadHtml(HTML);
            recurseElements(htmlDoc.DocumentNode.ChildNodes, fields);
        }

        public void recurseElements(HtmlNodeCollection nodes, Dictionary fields)
        {
            foreach (HtmlNode node in nodes)
            {
                if (node.HasChildNodes)
                    recurseElements(node.ChildNodes, fields);
                else
                {
                    if (node.HasAttributes && node.Attributes.Contains("type") && (
                        node.Attributes"type".Value == "text"
                        || node.Attributes"type".Value == "hidden"
                        || node.Attributes"type".Value == "password"
                        || node.Attributes"type".Value == "submit"))
                    {
                        if (node.Attributes.Contains("name") && !fields.Keys.Contains(node.Attributes"name".Value))
                        {
                            fields.Add(node.Attributes"name".Value, (node.Attributes.Contains("value") ? node.Attributes"value".Value : ""));
                        }
                    }
                }
            }
        }
    }
}