Поделиться через


Document Class

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

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
      DocumentFormat.OpenXml.OpenXmlPartRootElement
        DocumentFormat.OpenXml.Wordprocessing.Document

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

Syntax

'Declaration
<ChildElementInfoAttribute(GetType(Body))> _
<ChildElementInfoAttribute(GetType(DocumentBackground))> _
Public Class Document _
    Inherits OpenXmlPartRootElement
'Usage
Dim instance As Document
[ChildElementInfoAttribute(typeof(Body))]
[ChildElementInfoAttribute(typeof(DocumentBackground))]
public class Document : OpenXmlPartRootElement

Remarks

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:

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

Examples

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();
        }
    }
}
Imports DocumentFormat.OpenXml
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Module Module1
    Sub Main(ByVal args As String())
        Dim fileName As String = "C:\users\public\documents\DocumentEx.docx"

        ' Create a document. 
        Using myDocument As WordprocessingDocument = WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document)
            ' Add a main part. 
            Dim mainPart As MainDocumentPart = myDocument.AddMainDocumentPart()

            ' Create the document structure.
            mainPart.Document = New Document()
            Dim body As Body = mainPart.Document.AppendChild(New Body())
            Dim para As Paragraph = body.AppendChild(New Paragraph())
            Dim run As Run = para.AppendChild(New Run())

            ' Add some text to the document.
            run.AppendChild(New Text("Hello, World!"))
        End Using
        Console.WriteLine("The document has been created." & vbLf & "Press a key.")
        Console.ReadKey()
    End Sub
End Module

Thread Safety

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

See Also

Reference

Document Members

DocumentFormat.OpenXml.Wordprocessing Namespace