HTML Form Editor – C#

HTML Form Element

The HTML Form Element represents a section containing input controls for submitting user information. Using our library, you can easily fulfill the HTML forms and send data directly from your C# application. To make this process much more comfortable, we designed the Aspose.Html.Forms namespace. The main element of this namespace is FormEditor. It allows you to find an existing form in the document, create a new one from scratch, iterate through the elements, fill them up with data, and surely send the information to the remote server.

In this article, you will learn how to work with a FormEditor object to manipulate HTML form in the document, input controls, and submit information. For the testing purpose, we use the form designed by https://httpbin.org service.

How to Use FormEditor

Let’s look at the C# code snippet that demonstrates how to use the Aspose.Html.Forms namespace to programmatically fill and submit an HTML form. You need to follow the next steps:

  1. Create a new instance of the HTMLDocument class using HTMLDocument(url) constructor. Specify the URL of the HTML form to be filled out and submitted.
  2. Use the Create(document, index) method to create an instance of the FormEditor class, passing in it the HTMLDocument instance and the index of the form being edited (value 0 refers to the first form in the HTML document).
  3. Fill the data up using direct access to the input elements and save the filled HTML form to a file. Form fields are accessed and modified using the editor object.
  4. Use the FormSubmitter(editor) constructor to create an instance of the FormSubmitter class.
  5. Call the Submit() method of the FormSubmitter class to submit the form.
  6. Check the status of the form submission result using the IsSuccess property. If the form submission is successful, the response from the server is inspected:
    • if the response is in JSON format, the JSON content is printed to the console;
    • otherwise, the response is loaded into another HTMLDocument instance using the LoadDocument() method for further inspection.
 1// For complete examples and data files, please go to https://github.com/aspose-html/Aspose.HTML-for-.NET
 2// Initialize an instance of HTML document from 'https://httpbin.org/forms/post' url
 3using (var document = new Aspose.Html.HTMLDocument(@"https://httpbin.org/forms/post"))
 4{
 5    // Create an instance of Form Editor
 6    using (var editor = Aspose.Html.Forms.FormEditor.Create(document, 0))
 7    {
 8        // You can fill the data up using direct access to the input elements, like this:
 9        editor["custname"].Value = "John Doe";
10
11        document.Save("out.html");
12
13        // or this:
14        var comments = editor.GetElement<Aspose.Html.Forms.TextAreaElement>("comments");
15        comments.Value = "MORE CHEESE PLEASE!";
16
17        // or even by performing a bulk operation, like this one:
18        editor.Fill(new Dictionary<string, string>()
19        {
20            {"custemail", "john.doe@gmail.com"},
21            {"custtel", "+1202-555-0290"}
22        });
23
24        // Create an instance of form submitter
25        using (var submitter = new Aspose.Html.Forms.FormSubmitter(editor))
26        {
27            // Submit the form data to the remote server.
28            // If you need you can specify user-credentials and timeout for the request, etc.
29            var result = submitter.Submit();
30
31            // Check the status of the result object
32            if (result.IsSuccess)
33            {
34                // Check whether the result is in json format
35                if (result.ResponseMessage.Headers.ContentType.MediaType == "application/json")
36                {
37                    // Dump result data to the console
38                    Console.WriteLine(result.Content.ReadAsString());
39                }
40                else
41                {
42                    // Load the result data as an HTML document
43                    using (var resultDocument = result.LoadDocument())
44                    {
45                        // Inspect HTML document here.
46                    }
47                }
48
49            }
50        }
51    }
52}

Aspose.HTML offers HTML Web Applications that are an online collection of free converters, mergers, SEO tools, HTML code generators, URL tools, and more. The applications work on any operating system with a web browser and do not require any additional software installation. Easily convert, merge, encode, generate HTML code, extract data from the web, or analyze web pages in terms of SEO wherever you are. Use our collection of HTML Web Applications to perform your daily matters and make your workflow seamless!

Text “Banner HTML Web Applications”

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.