Validate a word processing document
This topic shows how to use the classes in the Open XML SDK for Office to programmatically validate a word processing document.
How the Sample Code Works
This code example consists of two methods. The first method, ValidateWordDocument, is used to validate a regular Word file. It doesn't throw any exceptions and closes the file after running the validation check. The second method, ValidateCorruptedWordDocument, starts by inserting some text into the body, which causes a schema error. It then validates the Word file, in which case the method throws an exception on trying to open the corrupted file. The validation is done by using the Validate method. The code displays information about any errors that are found, in addition to the count of errors.
Sample Code
In your main method, you can call the two methods, ValidateWordDocument and ValidateCorruptedWordDocument by using the following example that validates a file named "Word18.docx.".
string filepath = @"C:\Users\Public\Documents\Word18.docx";
ValidateWordDocument(filepath);
Console.WriteLine("The file is valid so far.");
Console.WriteLine("Inserting some text into the body that would cause Schema error");
Console.ReadKey();
ValidateCorruptedWordDocument(filepath);
Console.WriteLine("All done! Press a key.");
Console.ReadKey();
Important
Notice that you cannot run the code twice after corrupting the file in the first run. You have to start with a new Word file.
Following is the complete sample code in both C# and Visual Basic.
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;
using System;
ValidateCorruptedWordDocument(args[0]);
static void ValidateWordDocument(string filepath)
{
using (WordprocessingDocument wordprocessingDocument = WordprocessingDocument.Open(filepath, true))
{
try
{
OpenXmlValidator validator = new OpenXmlValidator();
int count = 0;
foreach (ValidationErrorInfo error in
validator.Validate(wordprocessingDocument))
{
count++;
Console.WriteLine("Error " + count);
Console.WriteLine("Description: " + error.Description);
Console.WriteLine("ErrorType: " + error.ErrorType);
Console.WriteLine("Node: " + error.Node);
if (error.Path is not null)
{
Console.WriteLine("Path: " + error.Path.XPath);
}
if (error.Part is not null)
{
Console.WriteLine("Part: " + error.Part.Uri);
}
Console.WriteLine("-------------------------------------------");
}
Console.WriteLine("count={0}", count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
wordprocessingDocument.Dispose();
}
}
static void ValidateCorruptedWordDocument(string filepath)
{
// Insert some text into the body, this would cause Schema Error
using (WordprocessingDocument wordprocessingDocument =
WordprocessingDocument.Open(filepath, true))
{
if (wordprocessingDocument.MainDocumentPart is null || wordprocessingDocument.MainDocumentPart.Document.Body is null)
{
throw new ArgumentNullException("MainDocumentPart and/or Body is null.");
}
// Insert some text into the body, this would cause Schema Error
Body body = wordprocessingDocument.MainDocumentPart.Document.Body;
Run run = new Run(new Text("some text"));
body.Append(run);
try
{
OpenXmlValidator validator = new OpenXmlValidator();
int count = 0;
foreach (ValidationErrorInfo error in
validator.Validate(wordprocessingDocument))
{
count++;
Console.WriteLine("Error " + count);
Console.WriteLine("Description: " + error.Description);
Console.WriteLine("ErrorType: " + error.ErrorType);
Console.WriteLine("Node: " + error.Node);
if (error.Path is not null)
{
Console.WriteLine("Path: " + error.Path.XPath);
}
if (error.Part is not null)
{
Console.WriteLine("Part: " + error.Part.Uri);
}
Console.WriteLine("-------------------------------------------");
}
Console.WriteLine("count={0}", count);
}
catch (Exception ex)
{
Console.WriteLine(ex.Message);
}
}
}