How to: Validate an Office Open XML Package by Using the Open XML API

This content is outdated and is no longer being maintained. It is provided as a courtesy for individuals who are still using these technologies. This page may contain URLs that were valid when originally published, but now link to sites or pages that no longer exist.

The Office Open XML Package specification defines a set of XML files that contain the content and define the relationships for all of the document parts stored in a single package. These packages combine the parts that comprise the document files for Microsoft® Office Excel® 2007, Microsoft Office PowerPoint® 2007, and Microsoft Office Word 2007. The Open XML Application Programming Interface (API) allows you to create packages and manipulate the files that comprise the packages. This topic walks through the code and steps to validate an Office Open XML package in Office Word 2007, although the steps are the same for each of the three 2007 Microsoft Office system programs that support the Office Open XML Format.

NoteNote

The code samples in this topic are in Microsoft Visual Basic® .NET and Microsoft Visual C#®. You can use them in an add-in created in Microsoft Visual Studio® 2008. For more information about how to create an add-in in Visual Studio 2008, see Getting Started with the Open XML Format SDK 1.0.

Validating an Office Open XML Package

In the following code, you validate a SpreadsheetDocument package by calling the Validate method of the Open XML object model.

' How to validate a package.
Public Sub ValidateSimplePackage(ByVal xlsxFile As String)
   Dim xlsxDoc As SpreadsheetDocument = SpreadsheetDocument.Open(xlsxFile, True)
   Using (xlsxDoc)
      ' Remove the main workbook part.
      xlsxDoc.DeletePart(xlsxDoc.WorkbookPart)
      ' Validate the package. This will return an exception 
      ' because of the missing document part.
      xlsxDoc.Validate(Nothing)
    End Using
End Sub
// How to validate a package.
public void ValidateSimplePackage(string xlsxFile)
{
   SpreadsheetDocument xlsxDoc = SpreadsheetDocument.Open(xlsxFile, true);
   Using(xlsxDoc)
   {
      // Remove the main workbook part.
      xlsxDoc.DeletePart(xlsxDoc.WorkbookPart);

      // Validate the package. This will return an exception 
      // because of the missing document part.
      xlsxDoc.Validate(null);
   }
}

To validate a SpreadsheetDocument package by calling the Validate method of the Open XML object model

  1. First, declare a variable that points to the document file.

  2. Next, open the package using the Open method. The package is opened with write permissions.

  3. Next, remove the workbooks part of the document using the Delete method.

  4. Finally, call the Validate method of the SpreadsheetDocument object to validate the package.

    NoteNote

    The Validate method does not validate the structure of an Office Open XML Package. For more information, see How to: Validate an Office Open XML Package by Using the Open XML API.

Custom Validation

To have full programmatic control over validation, or to perform complex validation checks, you should use the OpenXmlPackageValidationSettings event handler built into the Open XML object model.

' How to create custom validation.
Private Sub PackageValidationEventHandler(ByVal sender As Object, ByVal e As OpenXmlPackageValidationEventArgs)
    Console.Beep()
    Console.WriteLine("--------------------")
    Console.WriteLine("OpenXmlPackageValidationError:")
    Console.WriteLine(("Message = " + e.Message))
    If (Not (e.PartClassName) Is Nothing) Then
        Console.WriteLine(("PartClassName = " + e.PartClassName))
    End If
    If (Not (e.Part) Is Nothing) Then
        Console.WriteLine((e.Part.Uri))
    End If
    If (Not (e.SubPart) Is Nothing) Then
        Console.WriteLine((e.SubPart.Uri))
    End If
    If (Not (e.Message) Is Nothing) Then
        Console.WriteLine(("OpenXmlPartProblem = " + e.Message))
    End If
    Console.WriteLine("--------------------")
    Console.WriteLine("")
End Sub

Private Sub ValidatePackage(ByVal xlsxFile As String)
    ' Pass validation processing to the event.     
    Dim validationSettings As OpenXmlPackageValidationSettings = New OpenXmlPackageValidationSettings
    AddHandler validationSettings.EventHandler, AddressOf PackageValidationEventHandler
    Dim xlsxDoc As SpreadsheetDocument = SpreadsheetDocument.Open(xlsxFile, True)
    Using (xlsxDoc)
        xlsxDoc.DeletePart(xlsxDoc.WorkbookPart)
        ' Validate the package.
        xlsxDoc.Validate(validationSettings)
    End Using
End Sub
// How to create custom validation.
static void PackageValidationEventHandler(Object sender, OpenXmlPackageValidationEventArgs e)
{
    Console.Beep();
    Console.WriteLine("--------------------");
    Console.WriteLine("OpenXmlPackageValidationError:");
    Console.WriteLine("Message = " + e.Message);
    if (e.PartClassName != null)
    {
        Console.WriteLine("PartClassName = " + e.PartClassName);
    }
    if (e.Part != null)
    {
        Console.WriteLine(e.Part);
    }
    if (e.SubPart != null) 
    {
        Console.WriteLine(e.SubPart);
    }
    if (e.Message != null)
    {
        Console.WriteLine("OpenXmlPartProblem = " + e.Message);
    }
    Console.WriteLine("--------------------");
    Console.WriteLine("");
}
        
static void ValidatePackage()
{
    // Pass validation processing to the event.    OpenXmlPackageValidationSettings validationSettings = new OpenXmlPackageValidationSettings();
    validationSettings.EventHandler += new EventHandler<OpenXmlPackageValidationEventArgs>(PackageValidationEventHandler);

    string xlsxFile = @"c:\Book1.xlsx";
    SpreadsheetDocument xlsxDoc = SpreadsheetDocument.Open(xlsxFile, true);
    xlsxDoc.DeletePart(xlsxDoc.WorkbookPart);
    xlsxDoc.Save();

    // Validate the package.
    xlsxDoc.Validate(validationSettings);
}

To perform complex validation checks

  1. First, create a validation event handler for the custom package that gets the OpenXmlPackageValidationEventArgs parameter.

  2. Next, write custom code that calls the Validate method of a SpreadsheetDocument object.

  3. Then, you pass validation processing to the PackageValidationEventHandler event handler.

  4. Next, you declare a variable that points to the document file.

  5. Open the package using the Open method. The package opens with write permissions.

  6. Next, remove the Workbooks part of the document using the Delete method and save the changes using the Save method.

  7. Finally, call the Validate method of the SpreadsheetDocument object and pass the OpenXmlPackageValidationSettings parameter to validate the package.