How to Serialize Input Value – C# Example

Sometimes it is necessary to save an HTML document with input form fields filled in. Although browsers don’t allow this for security reasons, Aspose.HTML provides a SerializeInputValue property of the HTMLSaveOptions class that gives you this ability. If the HTML document has input fields filled in, using the SerializeInputValue property, you can save the resulting document as it looks in the browser.

This article describes how to serialize input values in HTML forms or input fields using the Aspose.HTML for .NET library.

Steps to Serialize Input Values

Some HTML documents contain form elements such as HTMLInputElement, HTMLTextAreaElement, and so on. For security reasons, their values are not saved, but you can change the situation using the SerializeInputValue option of the HTMLSaveOptions class. You should follow these required steps:

  1. Load an HTML document using the HTMLDocument class. You can load HTML from a file, HTML code, stream, or URL.
  2. Use one of the GetElement* methods of the Document class to get a required HTML element/elements in the document and use the Value properties (get or set their values).
  3. Create an instance of the HTMLSaveOptions class and set the SerializeInputValue property to true.
  4. Call the Save(path, saveOptions) method of the HTMLDocument class and pass the file path where you want to save the document and the HTMLSaveOptions instance as parameters.

How to Serialize Input Value in HTML Form

Let’s look at the apply the SerializeInputValue property using an example of an HTMLInputElement that represents an HTML input element such as a text box. Follow the step-by-step instructions to serialize the input value into a simple HTML form we create from scratch:

  1. Prepare HTML code.
  2. In the example, we create an HTML document from a string content using HTMLDocument (content, baseUri) constructor.
  3. Use the GetElementsByTagName() method of the Document class to select all the input elements in the document and store them in the inputElements variable.
  4. Take the first and only <input> element, in this case, from the resulting collection and set the Value property of the HTMLInputElement class.
  5. Save the HTML document to a file using the Save(path, saveOptions) method. Set the SerializeInputValue property to true on the HTMLSaveOptions object passed as a parameter to the Save() method.

Here is an example of how to serialize input values in an input field using Aspose.HTML C# library:

 1using System.IO;
 2using Aspose.Html;
 3using Aspose.Html.Saving;
 6	var html = @"
 7    <html>
 8        <body>
 9            <div>The new input element value: <input type = ""text"" value=""No"" /></div>
10        </body>
11    </html>";
13    // Create an HTML document from string of code containing an HTMLInputElement
14    using var doc = new HTMLDocument(html, string.Empty);
16    // Get all elements with the <input> tag
17    var inputElements = doc.GetElementsByTagName("input");
19    // Take the first and only element, in this case, from the resulting collection
20    var input = (HTMLInputElement)inputElements[0];
22    // Set the desired value for this HTML form element
23    input.Value = "Text";
25    // Prepare path to save HTML 
26    string savePath = Path.Combine(OutputDir, "result.html");
28    // Save the HTML document with SerializeInputValue set to true
29    doc.Save(savePath, new HTMLSaveOptions { SerializeInputValue = true });

Thus, the SerializeInputValue property of the HTMLSaveOptions class determines whether the values of input elements in an HTML document are serialized and included in the saved file or not. If SerializeInputValue is true, the input element values will be included in the saved file. If SerializeInputValue is not set, the input element values will not be included in the saved file.

You can download data files and complete C# examples that demonstrate how to set font folder from GitHub.

Aspose.HTML offers free HTML Web Applications, an online collection of free converters, mergers, SEO tools, HTML code generators, URL tools, formatters, minifiers, and more. The applications work on any operating system with a web browser and do not require any additional software installation. It’s a fast and easy way to efficiently and effectively solve your HTML-related tasks!

Subscribe to Aspose Product Updates

Get monthly newsletters & offers directly delivered to your mailbox.