Document Class

Document. It is the root element of MainDocumentPart.When the object is serialized out as xml, its qualified name is w:document.

Namespace:  DocumentFormat.OpenXml.Wordprocessing
Assembly:  DocumentFormat.OpenXml (in DocumentFormat.OpenXml.dll)

public class Document : OpenXmlPartRootElement

The following table lists the possible child types:

  • DocumentBackground <w:background>

  • Body <w:body>

[ISO/IEC 29500-1 1st Edition]

17.2.3 document (Document)

This element specifies the contents of a main document part in a WordprocessingML document.

[Example: Consider the basic structure of the main document part in a basic WordprocessingML document, as follows:


All of the contents of the main document part are contained beneath the document element. end example]

Parent Elements

Root element of WordprocessingML Main Document part

Child Elements


background (Document Background)


body (Document Body)




conformance (Document Conformance Class)

Specifies the conformance class (§2.4) to which the WordprocessingML document conforms.

If this attribute is omitted, its default value is transitional.

[Example: Consider the following WordprocessingML Main Document part markup:

<w:document w:conformance="strict">

This document has a conformance attribute value of strict, therefore it conforms to the WML Strict conformance class. end example]

The possible values for this attribute are defined by the ST_ConformanceClass simple type (§

[Note: The W3C XML Schema definition of this element’s content model (CT_Document) is located in §A.1. end note]

© ISO/IEC29500: 2008.

The following code example creates a word-processing document named "DocumentEx.docx" in the supplied path.

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

namespace DocumentEx
    class Program
        static void Main(string[] args)
            string fileName = @"C:\users\public\documents\DocumentEx.docx";

            // Create a document. 
            using (WordprocessingDocument myDocument =
                WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document))
                // Add a main part. 
                MainDocumentPart mainPart = myDocument.AddMainDocumentPart();

                // Create the document structure.
                mainPart.Document = new Document();
                Body body = mainPart.Document.AppendChild(new Body());
                Paragraph para = body.AppendChild(new Paragraph());
                Run run = para.AppendChild(new Run());

                // Add some text to the document.
                run.AppendChild(new Text("Hello, World!"));
            Console.WriteLine("The document has been created.\nPress a key.");

Any public static (Shared in Visual Basic) members of this type are thread safe. Any instance members are not guaranteed to be thread safe.

Community Additions