Export (0) Print
Expand All

Document class

Document. 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)

[ChildElementInfoAttribute(typeof(DocumentBackground))]
[ChildElementInfoAttribute(typeof(Body))]
public class Document : OpenXmlPartRootElement

[ISO/IEC 29500-1 1st Edition]

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:

<w:document>
  <w:body>
    <w:p/>
  </w:body>
</w:document>

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

Subclause

background (Document Background)

§17.2.1

body (Document Body)

§17.2.2

Attributes

Description

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">
  …</w:document>

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 (§22.9.2.2).

[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.");
            Console.ReadKey();
        }
    }
}

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

Show:
© 2014 Microsoft