Question

I'm developing a solution that allows people to upload a DOCX file as a template. This template is used for generating Word documents with database info.

What I would like to do is once a template gets uploaded, to check it for errors. (I don't want my parser crashing when a template is used.)

I've seen the question about checking a signature of a Word template, but that isn't enough to validate the integrity of the file. Of course it is possible to try to unzip the file, validate the XML in there, and so on, but this is rather CPU intensive and I'd like a different approach if there is one.

Are there any solutions that are part of the Open XML SDK or other standard approaches to this? Any ideas are apreciated.

Was it helpful?

Solution

in C# off the MSDN site

public static bool IsDocumentValid(WordprocessingDocument mydoc)
{
    OpenXmlValidator validator = new OpenXmlValidator();
    var errors = validator.Validate(mydoc);
    foreach (ValidationErrorInfo error in errors)
        Debug.Write(error.Description);
    return (errors.Count() == 0);
}
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top