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:
- Load an HTML document using the HTMLDocument class. You can load HTML from a file, HTML code, stream, or URL.
- Use one of the
GetElement*methods of the Document class to get a required HTML element/elements in the document and use theValueproperties (get or set their values). - Create an instance of the
HTMLSaveOptions class and set the
SerializeInputValueproperty totrue. - Call the
Save(
path,saveOptions) method of theHTMLDocumentclass and pass the file path where you want to save the document and theHTMLSaveOptionsinstance 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:
- Prepare HTML code.
- In the example, we create an HTML document from a string content using
HTMLDocument (
content,baseUri) constructor. - Use the
GetElementsByTagName() method of the Document class to select all the
inputelements in the document and store them in theinputElementsvariable. - Take the first and only
<input>element, in this case, from the resulting collection and set the Value property of the HTMLInputElement class. - Save the HTML document to a file using the
Save(
path,saveOptions) method. Set the SerializeInputValue property totrueon theHTMLSaveOptionsobject passed as a parameter to theSave()method.
Here is an example of how to serialize input values in an input field using Aspose.HTML C# library:
1// Set input value and serialize HTML form element using C#
2
3string html = @"
4<html>
5 <body>
6 <div>The new input element value: <input type = ""text"" value=""No"" /></div>
7 </body>
8</html>";
9
10// Create an HTML document from string of code containing an HTMLInputElement
11using HTMLDocument doc = new HTMLDocument(html, string.Empty);
12
13// Get all elements with the <input> tag
14HTMLCollection inputElements = doc.GetElementsByTagName("input");
15
16// Take the first and only element, in this case, from the resulting collection
17HTMLInputElement input = (HTMLInputElement)inputElements[0];
18
19// Set the desired value for this HTML form element
20input.Value = "Text";
21
22// Prepare a path to save HTML
23string savePath = Path.Combine(OutputDir, "result.html");
24
25// Save the HTML document with SerializeInputValue set to true
26doc.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!