MainDocumentPart Class

Defines the MainDocumentPart.

Inheritance Hierarchy


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


Public Class MainDocumentPart _
    Inherits OpenXmlPart
Dim instance As MainDocumentPart
public class MainDocumentPart : OpenXmlPart


The following code example opens a word-processing document, instantiates the document main part, reads the comments from the comment part, and displays them.

using System;
using System.IO;
using DocumentFormat.OpenXml.Packaging;

namespace MainDocumentPartEx
    class Program
        // Get the contents of a document part.
        static void Main(string[] args)
            string document = @"C:\Users\Public\Documents\MainDocumentPartEx.docx";
            string comments = null;

            // Open the file read-only.
            using (WordprocessingDocument wordprocessingDocument = 
                WordprocessingDocument.Open(document, false))
                MainDocumentPart mainPart = wordprocessingDocument.MainDocumentPart;
                WordprocessingCommentsPart WordprocessingCommentsPart = 

                // Read the comments using a stream reader.
                using (StreamReader streamReader = 
                    new StreamReader(WordprocessingCommentsPart.GetStream()))
                    comments = streamReader.ReadToEnd();
Imports System.IO
Imports DocumentFormat.OpenXml.Packaging

Module Module1
    ' Get the contents of a document part.
    Sub Main(ByVal args As String())
        Dim document As String = "C:\Users\Public\Documents\MainDocumentPartEx.docx"
        Dim comments As String = Nothing

        ' Open the file read-only.
        Using wordprocessingDocument As WordprocessingDocument = wordprocessingDocument.Open(document, False)
            Dim mainPart As MainDocumentPart = wordprocessingDocument.MainDocumentPart
            Dim WordprocessingCommentsPart As WordprocessingCommentsPart = mainPart.WordprocessingCommentsPart

            ' Read the comments using a stream reader.
            Using streamReader As New StreamReader(WordprocessingCommentsPart.GetStream())
                comments = streamReader.ReadToEnd()
            End Using
        End Using
    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


MainDocumentPart Members

DocumentFormat.OpenXml.Packaging Namespace