验证字处理文档

本主题演示如何使用 Open XML SDK for Office 中的类以编程方式验证字处理文档。


示例代码的工作方式

此代码示例由两个方法组成。 第一个方法 ValidateWordDocument 用于验证常规 Word 文件。 它不引发任何异常,并在运行验证检查后关闭文件。 第二个方法 ValidateCorruptedWordDocument 首先在正文中插入一些文本,这会导致架构错误。 然后它验证 Word 文件,在这种情况下尝试打开损坏的文件时方法会引发异常。 使用 方法完成 Validate 验证。 代码显示有关找到的所有错误的信息,以及错误计数。


重要

[!重要信息] 请注意,第一次运行时损坏文件后无法两次运行代码。 您必须从新 Word 文件开始。

以下是使用 C# 和 Visual Basic 编写的完整示例代码。

using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Validation;
using DocumentFormat.OpenXml.Wordprocessing;
using System;


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);
        }
    }
}

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);
        }
    }
}

另请参阅