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:
- 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.
- 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).
- 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. - Use the FormSubmitter(editor) constructor to create an instance of the FormSubmitter class.
- Call the Submit() method of the FormSubmitter class to submit the form.
- 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// Initialize an instance of HTML document from 'https://httpbin.org/forms/post' url
2using (var document = new Aspose.Html.HTMLDocument(@"https://httpbin.org/forms/post"))
3{
4 // Create an instance of Form Editor
5 using (var editor = Aspose.Html.Forms.FormEditor.Create(document, 0))
6 {
7 // You can fill the data up using direct access to the input elements, like this:
8 editor["custname"].Value = "John Doe";
9
10 document.Save("out.html");
11
12 // or this:
13 var comments = editor.GetElement<Aspose.Html.Forms.TextAreaElement>("comments");
14 comments.Value = "MORE CHEESE PLEASE!";
15
16 // or even by performing a bulk operation, like this one:
17 editor.Fill(new Dictionary<string, string>()
18 {
19 {"custemail", "john.doe@gmail.com"},
20 {"custtel", "+1202-555-0290"}
21 });
22
23 // Create an instance of form submitter
24 using (var submitter = new Aspose.Html.Forms.FormSubmitter(editor))
25 {
26 // Submit the form data to the remote server
27 // If you need you can specify user-credentials and timeout for the request, etc.
28 var result = submitter.Submit();
29
30 // Check the status of the result object
31 if (result.IsSuccess)
32 {
33 // Check whether the result is in json format
34 if (result.ResponseMessage.Headers.ContentType.MediaType == "application/json")
35 {
36 // Dump result data to the console
37 Console.WriteLine(result.Content.ReadAsString());
38 }
39 else
40 {
41 // Load the result data as an HTML document
42 using (var resultDocument = result.LoadDocument())
43 {
44 // Inspect HTML document here.
45 }
46 }
47
48 }
49 }
50 }
51}
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!