Document Solutions for Word
Report Templates / Template Validation
In This Topic
    Template Validation
    In This Topic

    When processing a template, there may be instances of errors. It may be difficult to identify all errors at once, and it may take a long time to fix one error at a time. DsWord provides TemplateError class and Validate method in DataTemplate class that enable a user to validate the templates in a document, defining which templates are malformed or wrongly used in the structure. When the user sets Validate to True, it marks erroneous templates in the document with the error information. When the user sets Validate to False, it only returns a list of errors.

    Refer to the following example code to validate the template:

    C#
    Copy Code
    // Initialize GcWordDocument.
    var doc = new GcWordDocument();
    
    // Add template data source.
    doc.DataTemplate.DataSources.Add("ds", new int[] { 1, 2, 3, 4, 5 });
                
    // Add template tags.
    doc.Body.Paragraphs.Add("{{ds.value}}");
    doc.Body.Paragraphs.Add("{{/ds}}");
    
    // Validate the template.
    var errors = doc.DataTemplate.Validate(true).ToList();
    
    // Save the Word document.
    doc.Save("Validate.docx");

    Limitations

    Validate method only identifies wrong template grammar and absent datasource path errors.