Share via


MainDocumentPart Class

Definition

Defines the MainDocumentPart

public class MainDocumentPart : DocumentFormat.OpenXml.Packaging.OpenXmlPart
public class MainDocumentPart : DocumentFormat.OpenXml.Packaging.TypedOpenXmlPart
[DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)]
public class MainDocumentPart : DocumentFormat.OpenXml.Packaging.OpenXmlPart
public class MainDocumentPart : DocumentFormat.OpenXml.Packaging.OpenXmlPart, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.AlternativeFormatImportPart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.CustomXmlPart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.EmbeddedControlPersistencePart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.EmbeddedObjectPart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.EmbeddedPackagePart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.ImagePart>, DocumentFormat.OpenXml.Packaging.ISupportedRelationship<DocumentFormat.OpenXml.Packaging.ThumbnailPart>
type MainDocumentPart = class
    inherit OpenXmlPart
type MainDocumentPart = class
    inherit TypedOpenXmlPart
[<DocumentFormat.OpenXml.OfficeAvailability(DocumentFormat.OpenXml.FileFormatVersions.Office2007)>]
type MainDocumentPart = class
    inherit OpenXmlPart
type MainDocumentPart = class
    inherit OpenXmlPart
    interface ISupportedRelationship<AlternativeFormatImportPart>
    interface ISupportedRelationship<CustomXmlPart>
    interface ISupportedRelationship<EmbeddedControlPersistencePart>
    interface ISupportedRelationship<EmbeddedObjectPart>
    interface ISupportedRelationship<EmbeddedPackagePart>
    interface ISupportedRelationship<ImagePart>
    interface ISupportedRelationship<ThumbnailPart>
Public Class MainDocumentPart
Inherits OpenXmlPart
Public Class MainDocumentPart
Inherits TypedOpenXmlPart
Public Class MainDocumentPart
Inherits OpenXmlPart
Implements ISupportedRelationship(Of AlternativeFormatImportPart), ISupportedRelationship(Of CustomXmlPart), ISupportedRelationship(Of EmbeddedControlPersistencePart), ISupportedRelationship(Of EmbeddedObjectPart), ISupportedRelationship(Of EmbeddedPackagePart), ISupportedRelationship(Of ImagePart), ISupportedRelationship(Of ThumbnailPart)
Inheritance
Inheritance
Attributes
Implements

Examples

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 =   
                    mainPart.WordprocessingCommentsPart;  

                // Read the comments using a stream reader.  
                using (StreamReader streamReader =   
                    new StreamReader(WordprocessingCommentsPart.GetStream()))  
                {  
                    comments = streamReader.ReadToEnd();  
                }  
            }  
            Console.WriteLine(comments);  
            Console.ReadKey();  
        }  
    }  
}  
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  
        Console.WriteLine(comments)  
        Console.ReadKey()  
    End Sub  
End Module  

Constructors

MainDocumentPart()

Creates an instance of the MainDocumentPart OpenXmlType

Properties

AlternativeFormatImportParts

Gets the AlternativeFormatImportParts of the MainDocumentPart

ChartParts

Gets the ChartParts of the MainDocumentPart

ContentType

Gets the content type (MIME type) of the content data in the part.

(Inherited from OpenXmlPart)
CustomizationPart

Gets the CustomizationPart of the MainDocumentPart

CustomXmlParts

Gets the CustomXmlParts of the MainDocumentPart

DataPartReferenceRelationships

Gets all DataPartReferenceRelationship relationships.

(Inherited from OpenXmlPartContainer)
DiagramColorsParts

Gets the DiagramColorsParts of the MainDocumentPart

DiagramDataParts

Gets the DiagramDataParts of the MainDocumentPart

DiagramLayoutDefinitionParts

Gets the DiagramLayoutDefinitionParts of the MainDocumentPart

DiagramPersistLayoutParts

Gets the DiagramPersistLayoutParts of the MainDocumentPart

DiagramStyleParts

Gets the DiagramStyleParts of the MainDocumentPart

Document

Gets or sets the root element of this part.

DocumentSettingsPart

Gets the DocumentSettingsPart of the MainDocumentPart

DocumentTasksPart

Gets the DocumentTasksPart of the MainDocumentPart

EmbeddedControlPersistenceParts

Gets the EmbeddedControlPersistenceParts of the MainDocumentPart

EmbeddedObjectParts

Gets the EmbeddedObjectParts of the MainDocumentPart

EmbeddedPackageParts

Gets the EmbeddedPackageParts of the MainDocumentPart

EndnotesPart

Gets the EndnotesPart of the MainDocumentPart

ExtendedChartParts

Gets the ExtendedChartParts of the MainDocumentPart

ExternalRelationships

Gets all external relationships. Hyperlink relationships are not included, use HyperlinkRelationship property to enumerate hyperlink relationships.

(Inherited from OpenXmlPartContainer)
Features

Gets the features associated with this part.

Features

Gets the features associated with this part.

(Inherited from OpenXmlPart)
FontTablePart

Gets the FontTablePart of the MainDocumentPart

FooterParts

Gets the FooterParts of the MainDocumentPart

FootnotesPart

Gets the FootnotesPart of the MainDocumentPart

GlossaryDocumentPart

Gets the GlossaryDocumentPart of the MainDocumentPart

HeaderParts

Gets the HeaderParts of the MainDocumentPart

HyperlinkRelationships

Gets all hyperlink relationships.

(Inherited from OpenXmlPartContainer)
ImageParts

Gets the ImageParts of the MainDocumentPart

IsRootElementLoaded

Gets a value indicating whether the root element is loaded from the part or it has been set.

(Inherited from OpenXmlPart)
Model3DReferenceRelationshipParts

Gets the Model3DReferenceRelationshipParts of the MainDocumentPart

NumberingDefinitionsPart

Gets the NumberingDefinitionsPart of the MainDocumentPart

OpenXmlPackage

Gets the OpenXmlPackage which contains the current part.

(Inherited from OpenXmlPart)
Parts

Gets all parts which are relationship targets of this part.

(Inherited from OpenXmlPartContainer)
RelationshipType

Gets the RelationshipType.

RootElement

Gets the root element of the current part. Returns null when the current part is empty or is not an XML content type.

(Inherited from OpenXmlPart)
StyleDefinitionsPart

Gets the StyleDefinitionsPart of the MainDocumentPart

StylesWithEffectsPart

Gets the StylesWithEffectsPart of the MainDocumentPart

ThemePart

Gets the ThemePart of the MainDocumentPart

ThumbnailPart

Gets the ThumbnailPart of the MainDocumentPart

Uri

Gets the internal part path in the package.

(Inherited from OpenXmlPart)
VbaProjectPart

Gets the VbaProjectPart of the MainDocumentPart

WebSettingsPart

Gets the WebSettingsPart of the MainDocumentPart

WordCommentsExtensiblePart

Gets the WordCommentsExtensiblePart of the MainDocumentPart

WordprocessingCommentsExPart

Gets the WordprocessingCommentsExPart of the MainDocumentPart

WordprocessingCommentsIdsPart

Gets the WordprocessingCommentsIdsPart of the MainDocumentPart

WordprocessingCommentsPart

Gets the WordprocessingCommentsPart of the MainDocumentPart

WordprocessingPeoplePart

Gets the WordprocessingPeoplePart of the MainDocumentPart

WordprocessingPrinterSettingsParts

Gets the WordprocessingPrinterSettingsParts of the MainDocumentPart

Methods

AddAlternativeFormatImportPart(AlternativeFormatImportPartType, String)

Adds a AlternativeFormatImportPart to the MainDocumentPart

AddAlternativeFormatImportPart(AlternativeFormatImportPartType)

Adds a AlternativeFormatImportPart to the MainDocumentPart

AddAlternativeFormatImportPart(String, String)

Adds a AlternativeFormatImportPart to the MainDocumentPart

AddAlternativeFormatImportPart(String)

Adds a AlternativeFormatImportPart to the MainDocumentPart

AddAnnotation(Object)

Adds an object to the annotation list of this PartContainer.

(Inherited from OpenXmlPartContainer)
AddCustomXmlPart(CustomXmlPartType, String)

Adds a CustomXmlPart to the MainDocumentPart

AddCustomXmlPart(CustomXmlPartType)

Adds a CustomXmlPart to the MainDocumentPart

AddCustomXmlPart(String, String)

Adds a CustomXmlPart to the MainDocumentPart

AddCustomXmlPart(String)

Adds a CustomXmlPart to the MainDocumentPart

AddEmbeddedControlPersistencePart(EmbeddedControlPersistencePartType, String)

Adds a EmbeddedControlPersistencePart to the MainDocumentPart

AddEmbeddedControlPersistencePart(EmbeddedControlPersistencePartType)

Adds a EmbeddedControlPersistencePart to the MainDocumentPart

AddEmbeddedControlPersistencePart(String, String)

Adds a EmbeddedControlPersistencePart to the MainDocumentPart

AddEmbeddedControlPersistencePart(String)

Adds a EmbeddedControlPersistencePart to the MainDocumentPart

AddEmbeddedObjectPart(EmbeddedObjectPartType, String)
AddEmbeddedObjectPart(EmbeddedObjectPartType)
AddEmbeddedObjectPart(String, String)
AddEmbeddedObjectPart(String)

Adds a EmbeddedObjectPart to the MainDocumentPart

AddEmbeddedPackagePart(EmbeddedPackagePartType, String)
AddEmbeddedPackagePart(EmbeddedPackagePartType)
AddEmbeddedPackagePart(String, String)
AddEmbeddedPackagePart(String)

Adds a EmbeddedPackagePart to the MainDocumentPart

AddExtendedPart(String, String, String, String)

Adds an extended part ( Application specific part ).

(Inherited from OpenXmlPartContainer)
AddExtendedPart(String, String, String)

Adds an extended part ( Application specific part ).

(Inherited from OpenXmlPartContainer)
AddExternalRelationship(String, Uri, String)

Adds an external relationship. Do not add hyperlink relationships through this method. Use AddHyperlinkRelationship() instead.

(Inherited from OpenXmlPartContainer)
AddExternalRelationship(String, Uri)

Adds an external relationship. Do not add hyperlink relationships through this method. Use AddHyperlinkRelationship() instead.

(Inherited from OpenXmlPartContainer)
AddHyperlinkRelationship(Uri, Boolean, String)

Adds a new hyperlink relationship.

(Inherited from OpenXmlPartContainer)
AddHyperlinkRelationship(Uri, Boolean)

Adds a new hyperlink relationship.

(Inherited from OpenXmlPartContainer)
AddImagePart(ImagePartType, String)

Adds a ImagePart to the MainDocumentPart

AddImagePart(ImagePartType)

Adds a ImagePart to the MainDocumentPart

AddImagePart(String, String)

Adds a ImagePart to the MainDocumentPart

AddImagePart(String)

Adds a ImagePart to the MainDocumentPart

AddNewPart<T>()

Adds a new part of type T.

(Inherited from OpenXmlPartContainer)
AddNewPart<T>(String, String)

Adds a new part of type T.

(Inherited from OpenXmlPartContainer)
AddNewPart<T>(String)

Adds a new part of type T.

(Inherited from OpenXmlPartContainer)
AddPart<T>(T, String)

Adds the part to the document with a given relationship identifier (ID). Must use the returned part to operate on the part added to the document

(Inherited from OpenXmlPartContainer)
AddPart<T>(T)

Adds the part to the document. Must use the returned part to operate on the part added to the document.

(Inherited from OpenXmlPartContainer)
AddThumbnailPart(String, String)

Adds a ThumbnailPart to the MainDocumentPart

AddThumbnailPart(String)

Adds a ThumbnailPart to the MainDocumentPart

AddThumbnailPart(ThumbnailPartType, String)

Adds a ThumbnailPart to the MainDocumentPart

AddThumbnailPart(ThumbnailPartType)

Adds a ThumbnailPart to the MainDocumentPart

AddVideoReferenceRelationship(MediaDataPart, String)

Adds a VideoReferenceRelationship to the MainDocumentPart

AddVideoReferenceRelationship(MediaDataPart)

Adds a VideoReferenceRelationship to the MainDocumentPart

Annotation(Type)

Get the first annotation object of the specified type from this PartContainer.

(Inherited from OpenXmlPartContainer)
Annotation<T>()

Get the first annotation object of the specified type from this PartContainer.

(Inherited from OpenXmlPartContainer)
Annotations(Type)

Gets a collection of annotations of the specified type for this PartContainer.

(Inherited from OpenXmlPartContainer)
Annotations<T>()

Gets a collection of annotations of the specified type for this PartContainer.

(Inherited from OpenXmlPartContainer)
ChangeIdOfPart(OpenXmlPart, String)

Changes the relationship ID of the part.

(Inherited from OpenXmlPartContainer)
CreateRelationshipToPart(OpenXmlPart, String)

Adds a relationship for the specified part to this part.

(Inherited from OpenXmlPartContainer)
CreateRelationshipToPart(OpenXmlPart)

Adds a relationship for the specified part to this part.

(Inherited from OpenXmlPartContainer)
DeleteExternalRelationship(ExternalRelationship)

Deletes the specified external relationship.

(Inherited from OpenXmlPartContainer)
DeleteExternalRelationship(String)

Deletes the specified ExternalRelationship.

(Inherited from OpenXmlPartContainer)
DeletePart(OpenXmlPart)

Deletes a specified part in the package root layer.

(Inherited from OpenXmlPartContainer)
DeletePart(String)

Deletes the specified child part from this part.

(Inherited from OpenXmlPartContainer)
DeleteParts<T>(IEnumerable<T>)

Deletes all the parts which are in the passed in collection from the document.

(Inherited from OpenXmlPartContainer)
DeleteReferenceRelationship(ReferenceRelationship)

Deletes the specified reference relationship.

(Inherited from OpenXmlPartContainer)
DeleteReferenceRelationship(String)

Deletes the specified reference relationship.

(Inherited from OpenXmlPartContainer)
FeedData(Stream)

Feeds data into the part stream. The stream of the part will be truncated at first.

(Inherited from OpenXmlPart)
GetExternalRelationship(String)

Gets the specified ExternalRelationship.

(Inherited from OpenXmlPartContainer)
GetIdOfPart(OpenXmlPart)

Gets the relationship ID of the part.

(Inherited from OpenXmlPartContainer)
GetParentParts()

Enumerates all parents that reference this part anywhere in the document.

(Inherited from OpenXmlPart)
GetPartById(String)

Gets the child part through the relationship ID.

(Inherited from OpenXmlPartContainer)
GetPartsCountOfType<T>()
Obsolete.

Gets the count of all parts of type T.

(Inherited from OpenXmlPartContainer)
GetPartsOfType<T>()

Enumerates all the children parts of the specified type T of this part.

(Inherited from OpenXmlPartContainer)
GetPartsOfType<T>(ICollection<T>)
Obsolete.

Gets all the children parts of the specified type T into partCollection of this part.

(Inherited from OpenXmlPartContainer)
GetReferenceRelationship(String)

Gets the specified ReferenceRelationship.

(Inherited from OpenXmlPartContainer)
GetStream()

Returns the part content data stream.

(Inherited from OpenXmlPart)
GetStream(FileMode, FileAccess)

Returns the part content stream that was opened using a specified FileMode and FileAccess.

(Inherited from OpenXmlPart)
GetStream(FileMode)

Returns the content stream that was opened using a specified I/O FileMode.

(Inherited from OpenXmlPart)
RemoveAnnotations(Type)

Removes the annotations of the specified type from this PartContainer.

(Inherited from OpenXmlPartContainer)
RemoveAnnotations<T>()

Removes the annotations of the specified type from this PartContainer.

(Inherited from OpenXmlPartContainer)
ThrowIfObjectDisposed()

Indicates whether the object is already disposed.

(Inherited from OpenXmlPart)
TryGetPartById(String, OpenXmlPart)

Try to get the child part by the relationship ID.

(Inherited from OpenXmlPartContainer)
UnloadRootElement()

Unloads the RootElement.

(Inherited from OpenXmlPart)
ValidateXml(String, ValidationEventHandler)
Obsolete.

Validates the XML content of the part by using the specified schema.

(Inherited from OpenXmlPart)
ValidateXml(XmlSchemaSet, ValidationEventHandler)
Obsolete.

Validates the XML content of the part by using the specified schema.

(Inherited from OpenXmlPart)

Extension Methods

AddDisposableFeature(OpenXmlPart)
AddPartRootEventsFeature(OpenXmlPartContainer)

Adds a feature to track eventing for package life cycle events.

CreateUnknownElement(OpenXmlPartContainer, String)

Creates a new OpenXmlUnknownElement class by using the outer XML.

AddImagePart(MainDocumentPart, String)
GetXDocument(OpenXmlPart)

Gets an XDocument representation of the part.

GetXElement(OpenXmlPart)

Gets an XElement representation of the part.

IsRootXElementLoaded(OpenXmlPart)

Gets a value indicating whether the root XElement is loaded from the part or it has been set.

SaveXDocument(OpenXmlPart)

Saves the current XDocument to the part if it and its Root element is not null.

SaveXElement(OpenXmlPart)

Saves the current XDocument to the part if it and its Root element is not null.

SetXDocument(OpenXmlPart, XDocument)

Sets the OpenXmlPart's XDocument to the given XDocument, serializes and writes the XDocument to the OpenXmlPart, and reloads the OpenXmlPart's OpenXmlPartRootElement if it was previously loaded.

SetXElement(OpenXmlPart, XElement)

Sets the OpenXmlPart's root XElement to the given XElement, serializes and writes the XElement to the OpenXmlPart, and reloads the OpenXmlPart's OpenXmlPartRootElement if it was previously loaded.

Applies to