Catch malformed tags in a Word template without filling it or saving a new document.
dotnet add package RecursiveAI.OneDocument.DocumentsRequires .NET 10 (net10.0); .NET 9 and earlier aren’t supported.
using RecursiveAI.OneDocument; // template.docx is in the download. var defects = Template.Validate("template.docx"); foreach (var defect in defects) { Console.WriteLine(defect.Location); Console.WriteLine(defect.Code); }
Unzip the example, then dotnet run.
To check your own .docx, replace "template.docx" with its path.
Prints the location and code of an unclosed placeholder; writes no document:
/word/document.xml#body/p2@5 template.unclosed-tag
The included template.docx has these two body paragraphs. The first tag is missing its closing braces.
Dear {{ customer Order {{ order }}
The full program
The download compares template.docx with fixed.docx. The fixed copy changes the first paragraph to Dear {{ customer }} and leaves the second paragraph unchanged.
using RecursiveAI.OneDocument; foreach (string path in new[] { "template.docx", "fixed.docx" }) { var defects = Template.Validate(path); Console.WriteLine($"{path}: {defects.Count} defect(s)"); foreach (var defect in defects) Console.WriteLine($"{defect.Location}: {defect.Code}\n {defect.Message}"); }
dotnet run prints:
template.docx: 1 defect(s) /word/document.xml#body/p2@5: template.unclosed-tag The tag opened here is never closed by '}}' in this paragraph or line. fixed.docx: 0 defect(s)
A malformed tag is returned in the defect list; a path that can’t be opened is a file error, not a syntax result.
Variations
Check the corrected template. Replace everything after the using in the snippet at the top with:
var defects = Template.Validate("fixed.docx"); Console.WriteLine($"Defects: {defects.Count}");
Defects: 0Related recipes
The short answer
Call Template.Validate(path) before Template.Render. It returns preparation errors and warnings with their locations, without binding data or changing the file. Use it when a template is submitted or edited, so the author can fix the tags before anyone tries to generate a document.
How do I check DOCX template syntax before rendering?
Validate the actual Word file, not an exported text copy. In the example, /word/document.xml points to the main document part, and body/p2@5 identifies a position inside it. The offset @5 lands after Dear, at the opening braces. A location is a path into the file, not a Word page number. Show the message alongside the path so the author knows what to fix, and use the code if your application needs to classify the problem.
OneDocument checks tag structure, including unclosed tags and blocks. The Word binder processes the body and other text areas such as headers, footers, and notes. A placeholder split across text runs inside one paragraph can still be read; a placeholder that starts in one paragraph and ends in another can’t. A visual line wrap isn’t a new paragraph. See the Word template docs for what text the binder reads.
Why aren’t placeholders replaced in my Word template?
Check syntax first. In this example, the unclosed tag needs a change to the DOCX, not a change to the data. A well-formed tag that names a missing field is a different problem: Template.Validate takes no data, so an empty list doesn’t prove that a given customer or order record has the fields your template needs.
To catch that second class of problem, check when rendering with the real data. By default, a missing value renders as empty text and returns a template.missing-data warning. Use TemplateOptions.Strict at render time to turn a reported missing value or expression error into TemplateException. That check complements the syntax pass; it doesn’t replace it. For example, the author can repair the braces once, but each incoming order still needs to supply the values its rendered document uses.
The corrected fixed.docx passes Validate, but this sample data lacks customer. Run this separately to see what the render check reports:
Template is in RecursiveAI.OneDocument; TemplateOptions is in RecursiveAI.OneDocument.Templating.
using RecursiveAI.OneDocument; using RecursiveAI.OneDocument.Templating; using System.Text.Json; using var data = JsonDocument.Parse("""{"order":42}"""); var options = new TemplateOptions { Strict = true }; try { Template.Render("fixed.docx", data, "filled.docx", options); } catch (TemplateException error) { Console.WriteLine(error.Message); }
Fatal template.missing-data at /word/document.xml#body/p2@8: No data named 'customer'.No filled.docx is created in this run.