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:

 1var html = @"
 2<html>
 3    <body>
 4        <div>The new input element value: <input type = ""text"" value=""No"" /></div>
 5    </body>
 6</html>";
 7
 8// Create an HTML document from string of code containing an HTMLInputElement
 9using var doc = new HTMLDocument(html, string.Empty);
10
11// Get all elements with the <input> tag
12var inputElements = doc.GetElementsByTagName("input");
13
14// Take the first and only element, in this case, from the resulting collection
15var input = (HTMLInputElement)inputElements[0];
16
17// Set the desired value for this HTML form element
18input.Value = "Text";
19
20// Prepare a path to save HTML 
21string savePath = Path.Combine(OutputDir, "result.html");
22
23// Save the HTML document with SerializeInputValue set to true
24doc.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 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.