Compartilhar via


OpenXmlElement Class

Defines the OpenXmlElement - base class for all elements in Open XML document. Defines the OpenXmlElement - base class for all elements in Open XML document.

Inheritance Hierarchy

System.Object
  DocumentFormat.OpenXml.OpenXmlElement
    DocumentFormat.OpenXml.OpenXmlCompositeElement
    DocumentFormat.OpenXml.OpenXmlLeafElement
    DocumentFormat.OpenXml.OpenXmlMiscNode

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

Syntax

'Declaration
Public MustInherit Class OpenXmlElement _
    Implements IEnumerable(Of OpenXmlElement), IEnumerable,  _
    ICloneable
'Usage
Dim instance As OpenXmlElement
public abstract class OpenXmlElement : IEnumerable<OpenXmlElement>, 
    IEnumerable, ICloneable

Remarks

Annotations will not be cloned when calling .Clone() and .CloneNode(bool)

Examples

The following code example shows how to access elements at the same level in a word-processing document. The test file used in the example contains the text “OpenXml Element.”

using System;
using System.Collections.Generic;
using System.Linq;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace OpenXmlElement
{
    class Program
    {
        // This code example shows how to access elements at the same level  
        // in a word-processing document. 
        // The example is using a file that contains the text "OpenXml Element."
        static void Main(string[] args)
        {
            string fileName = @"C:\Users\Public\Documents\AccessElementsSameLevel.docx";
            using (WordprocessingDocument wordprocessingDocument = 
                WordprocessingDocument.Open(fileName, false))
            {
                // Create a Body object.
                DocumentFormat.OpenXml.Wordprocessing.Body body =
                    wordprocessingDocument.MainDocumentPart.Document.Body;

                // Create a Paragraph object.
                DocumentFormat.OpenXml.Wordprocessing.Paragraph firstParagraph =
                    body.Elements<Paragraph>().FirstOrDefault();

                // Get the first child of an OpenXmlElement.
                 DocumentFormat.OpenXml.OpenXmlElement firstChild = firstParagraph.FirstChild;
                IEnumerable<Run> elementsAfter =
                    firstChild.ElementsAfter().Where(c => c is Run).Cast<Run>();

                // Get the Run elements after the specified element.
                Console.WriteLine("Run elements after the first child are: ");
                foreach (DocumentFormat.OpenXml.Wordprocessing.Run run in elementsAfter)
                {
                    Console.WriteLine(run.InnerText);
                }
                Console.ReadKey();
            }
        }
    }
}
// Output:
// Run elements after the first child are:
// OpenXml
//  Element
Imports System
Imports System.Collections.Generic
Imports System.Linq
Imports DocumentFormat.OpenXml.Packaging
Imports DocumentFormat.OpenXml.Wordprocessing

Module Module1
    ' This code example shows how to access elements at the same level  
    ' in a word-processing document. 
    ' The example is using a file that contains the text "OpenXml Element."
    Sub Main()
        Dim fileName As String = "C:\Users\Public\Documents\AccessElementsSameLevel.docx"
        Using wordprocessingDocument As WordprocessingDocument = _
    wordprocessingDocument.Open(fileName, False)
            ' Create a Body object.
            Dim body As DocumentFormat.OpenXml.Wordprocessing.Body = _
    wordprocessingDocument.MainDocumentPart.Document.Body

            ' Create a Paragraph object.
            Dim firstParagraph As DocumentFormat.OpenXml.Wordprocessing.Paragraph = _
    body.Elements(Of Paragraph)().FirstOrDefault()

            ' Get the first child of an OpenXmlElement.
            Dim firstChild As DocumentFormat.OpenXml.OpenXmlElement = _
                firstParagraph.FirstChild
            Dim elementsAfter As IEnumerable(Of Run) = _
                firstChild.ElementsAfter().Where(Function(c) TypeOf c Is Run).Cast(Of Run)()

            ' Get the Run elements before/after the specified element.
            Console.WriteLine("Run elements after the first child are: ")
            For Each run As DocumentFormat.OpenXml.Wordprocessing.Run In elementsAfter
                Console.WriteLine(run.InnerText)
            Next
            Console.ReadKey()
        End Using
    End Sub
End Module
' Output:
' Run elements after the first child are:
' OpenXml
'  Element

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

OpenXmlElement Members

DocumentFormat.OpenXml Namespace