Click to Rate and Give Feedback
MSDN
MSDN Library
Office Development
2007 Office Suites
How Do I...
 How to: Validate the Contents of a ...

  Switch on low bandwidth view
How to: Validate the Contents of a Document Part in an Office Open XML Package by Using the Open XML API

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 the contents of a document part in 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 2008see Getting Started with the Open XML Format SDK 1.0.

Validating the Contents of a Part in an Office Open XML Package

In the following code, you validate the XML content of the MainDocumentPart part of a WordprocessingDocument package by calling the ValidateXml method of MainDocumentPart part.

Visual Basic
' How to validate the contents of a document part.
Public Sub ValidDocumentContent(ByVal document As String, ByVal schemaFile As String)
    Dim wordDoc As WordprocessingDocument = WordprocessingDocument.Open(document, false)
    wordDoc.MainDocumentPart.ValidateXml(schemaFile, Nothing)
End Sub
C#
// How to validate the contents of a document part.
public static void ValidDocumentContent(string document, string schemaFile)
{
    using (WordprocessingDocument wordDoc = WordprocessingDocument.Open(document, false))
    {
        wordDoc.MainDocumentPart.ValidateXml(schemaFile, null);
    }
 }

To validate the XML content of the MainDocumentPart part by calling the ValidateXml method of MainDocumentPart part

  1. First, pass in parameters representing the path to and the name of the source Word 2007 document as well as a schema file.

  2. Then, open the document as a WordprocessingDocument object.

  3. Next, create a reference to the MainDocumentPart part and call the ValidateXml method.

  4. Finally, the ValidateXml method uses the schema file to validate the XML contents of the MainDocumentPart part.

    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.

© 2009 Microsoft Corporation. All rights reserved. Terms of Use  |  Trademarks  |  Privacy Statement
Page view tracker