Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
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.
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:
GetElement* methods of the
Document class to get the required form elements and set their Value properties.SerializeInputValue property to true.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.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:
content, baseUri) constructor.input elements in the document and store them in the inputElements variable.<input> element, in this case, from the resulting collection and set the
Value property of the
HTMLInputElement class.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.
| Issue | Why it happens | What to check |
|---|---|---|
| Filled input values are missing in the saved HTML | SerializeInputValue was not enabled before saving. | Create HTMLSaveOptions, set SerializeInputValue = true, and pass the options to Save(). |
| Only the original HTML attribute value is saved | The DOM value was not updated before saving. | Set the Value property on the target form element before calling Save(). |
| The wrong field is updated | The selector or element lookup returned a different input. | Check the element name, id, type, or index before assigning the value. |
SerializeInputValue tells Aspose.HTML to include current input field values when saving an HTML document with HTMLSaveOptions.
No. If SerializeInputValue is not enabled, current input values are not included in the saved HTML file.
Yes. Set the form element Value property first, then save the document with HTMLSaveOptions.SerializeInputValue enabled.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.