views:

171

answers:

1

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.

+3  A: 

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);
}
Ben Jones
Great, thank you :)
Jeroen Landheer