MainDocumentPart Class

Defines the MainDocumentPart.

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

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();

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