Serialize HTML Input Values in C#

To serialize HTML input values in C#, set form field values in the DOM, create HTMLSaveOptions, enable SerializeInputValue, and call HTMLDocument.Save(path, saveOptions).

Sometimes you need to save an HTML document with filled form fields. Browsers normally do not write current input values back into the HTML source, but Aspose.HTML for .NET provides the SerializeInputValue property of the HTMLSaveOptions class. When this option is enabled, current input values are included in the saved HTML document.

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

Steps to Serialize Input Values

Some HTML documents contain form elements such as HTMLInputElement, HTMLTextAreaElement, and other controls. Current form values are not saved unless the SerializeInputValue option is enabled. Follow these 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 the required form elements and set their Value properties.
  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 apply the SerializeInputValue property using an HTMLInputElement that represents a text input field. The example creates a simple HTML form from a string, sets the input value, and saves the filled form:

  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:

 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 input element values are serialized into the saved file. If SerializeInputValue is true, current input values are included in the saved HTML. If the option is not enabled, current input values are not written to the saved file.

Common Serialization Issues

IssueWhy it happensWhat to check
Filled input values are missing in the saved HTMLSerializeInputValue was not enabled before saving.Create HTMLSaveOptions, set SerializeInputValue = true, and pass the options to Save().
Only the original HTML attribute value is savedThe DOM value was not updated before saving.Set the Value property on the target form element before calling Save().
The wrong field is updatedThe selector or element lookup returned a different input.Check the element name, id, type, or index before assigning the value.

FAQ

What does SerializeInputValue do?

SerializeInputValue tells Aspose.HTML to include current input field values when saving an HTML document with HTMLSaveOptions.

Are form values serialized by default?

No. If SerializeInputValue is not enabled, current input values are not included in the saved HTML file.

Can I serialize values after editing them in code?

Yes. Set the form element Value property first, then save the document with HTMLSaveOptions.SerializeInputValue enabled.

Related Articles