Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.
To convert an HTML template to HTML in C#, call Converter.ConvertTemplate() with the template source, TemplateData, TemplateLoadOptions, and output path. Aspose.HTML for .NET replaces template expressions such as {{expression}} with values from XML or JSON data.
Aspose.HTML for .NET offers a set of
ConvertTemplate() methods that convert an HTML template into a complete HTML document. The methods can take a template file path or an HTMLDocument, a data source, load options, and the output file path.
An HTML template is HTML markup with placeholders for dynamic data. These placeholders are inline expressions enclosed in double curly braces, for example {{expression}}. During template processing, ConvertTemplate() substitutes expressions with values from the data source. The data source can be XML or JSON.
Once you have prepared an HTML template and a data source, you can populate the template in a single API call. Use the ConvertTemplate(sourcePath, data, options, outputPath) overload when the template is stored as a file.
To convert a template file to HTML:
Converter.ConvertTemplate(sourcePath, data, options, outputPath).1// Convert Template to HTML using C#
2
3// Convert template to HTML
4Converter.ConvertTemplate(
5 Path.Combine(DataDir, "template.html"),
6 new TemplateData(Path.Combine(DataDir, "data-source.json")),
7 new TemplateLoadOptions(),
8 Path.Combine(OutputDir, "template-with-single-line.html")
9);The example uses template.html as the source template, data-source.json as the data source, default TemplateLoadOptions, and saves the populated document as template-with-single-line.html.
Use the
ConvertTemplate(HTMLDocument, TemplateData, TemplateLoadOptions, string) overload when the template is already loaded as an HTMLDocument. The method takes four parameters:
HTMLDocument object used as the template source.TemplateData object that holds the data used to populate the template.TemplateLoadOptions object that provides template loading options.string value that specifies the full output HTML file path.To convert a loaded template document:
TemplateData object from the XML file.TemplateLoadOptions.Converter.ConvertTemplate(document, templateData, options, resultPath).HTMLDocument after conversion. 1// Populate HTML template with external XML data using C#
2
3// Prepare a path to an HTML source file
4string sourcePath = Path.Combine(DataDir, "template.html");
5
6// Prepare a path to an xml template data file
7string templateDataPath = Path.Combine(DataDir, "templateData.xml");
8
9// Define TemplateData object instance
10TemplateData templateData = new TemplateData(templateDataPath);
11
12// Prepare a path to the result file
13string resultPath = Path.Combine(OutputDir, "result.html");
14
15// Define default TemplateLoadOptions object
16TemplateLoadOptions options = new TemplateLoadOptions();
17
18// Initialize an HTML document as conversion source
19HTMLDocument document = new HTMLDocument(sourcePath, new Configuration());
20
21// Convert template to HTML
22Converter.ConvertTemplate(document, templateData, options, resultPath);
23
24// Clear resources
25document.Dispose();The code creates an
HTMLDocument from template.html and uses
Converter.ConvertTemplate() to populate it with data from templateData.xml. The resulting HTML document is saved to result.html, and the source document object is disposed after conversion.
You can also create both the data source and the template during runtime. This is useful when your application generates structured data and HTML markup before saving the final HTML output.
To create and convert an HTML template on the fly:
{{FirstName}}.TemplateData from the data source file.TemplateLoadOptions.sourcePath, data, options, and outputPath. 1// Populate an HTML template with structured JSON data using C#
2
3// Prepare a path to JSON data source file
4string jsonPath = Path.Combine(OutputDir, "data-source.json");
5
6// Prepare a JSON data source and save it to the file
7string data = @"{
8 ""FirstName"": ""John"",
9 ""LastName"": ""Doe"",
10 ""Address"": {
11 ""City"": ""Dallas"",
12 ""Street"": ""Austin rd."",
13 ""Number"": ""200""
14 }
15 }";
16File.WriteAllText(jsonPath, data);
17
18// Prepare a path to an HTML Template file
19string sourcePath = Path.Combine(OutputDir, "template.html");
20
21// Prepare an HTML Template and save it to the file
22string template = @"
23 <table border=1>
24 <tr>
25 <th>Person</th>
26 <th>Address</th>
27 </tr>
28 <tr>
29 <td>{{FirstName}} {{LastName}}</td>
30 <td>{{Address.Street}} {{Address.Number}}, {{Address.City}}</td>
31 </tr>
32 </table>
33 ";
34File.WriteAllText(sourcePath, template);
35
36// Prepare a path to the output file (data-filled template file)
37string outputPath = Path.Combine(OutputDir, "template-output.html");
38
39// Invoke Converter.ConvertTemplate() method in order to populate "template.html" with the data source from "data-source.json" file
40Converter.ConvertTemplate(sourcePath, new TemplateData(jsonPath), new TemplateLoadOptions(), outputPath);The example writes data-source.json and template.html, then calls Converter.ConvertTemplate() to create template-output.html. The template uses inline expressions such as {{FirstName}}, {{LastName}}, and {{Address.City}}, and the values are taken from the JSON data source.
You can download the complete examples and data files from GitHub.
Aspose.HTML for .NET can populate HTML templates from XML or JSON data sources by using TemplateData.
Use the file-path overload when the template is stored on disk. Use the HTMLDocument overload when the template is already loaded as a document object.
TemplateLoadOptions provides options for loading and matching template data during template processing.
Analyzing your prompt, please hold on...
An error occurred while retrieving the results. Please refresh the page and try again.