Text Class

Text.When the object is serialized out as xml, its qualified name is w:t.

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

public class Text : TextType

[ISO/IEC 29500-1 1st Edition] t (Text)

This element specifies that this run contains literal text which shall be displayed in the document. The t element shall be used for all text runs which are not:

  • Part of a region of text that is contained in a deleted region using the del element (§

  • Part of a region of text that is contained within a field code

[Example: Consider a paragraph of WordprocessingML content which reads This is text. This paragraph would therefore be represented as follows:

<w:t>This is text</w:t>

The text is contained in a t node. end example]

Parent Elements

r (§; r (§



xml:space (Content Contains Significant Whitespace)

Namespace: http://www.w3.org/XML/1998/namespace

Specifies how white space should be handled for the contents of this element using the W3C space preservation rules.

[Example: Consider the following run contained within a WordprocessingML document:

<w:t>   significant whitespace   </w:t>

Although there are three spaces on each side of the text content in the run, that whitespace has not been specifically marked as significant, therefore it is subject to the space preservation rules currently specified in that run's scope. end example]

The possible values for this attribute are defined by §2.10 of the XML 1.0 specification.

[Note: The W3C XML Schema definition of this element’s content model (CT_Text) is located in §A.1. end note]

© ISO/IEC29500: 2008.

The following code example creates a document named "TextEx.docx" at the specified path, and writes the phrase "Hello, World!" to it.

using System;
using DocumentFormat.OpenXml;
using DocumentFormat.OpenXml.Packaging;
using DocumentFormat.OpenXml.Wordprocessing;

namespace TextEx
    class Program
        static void Main(string[] args)
             string fileName = @"c:\users\public\documents\TextEx.docx";
             // Create a wordprocessing document with the specified file name.
            WordprocessingDocument wordprocessingDocument = 
                WordprocessingDocument.Create(fileName, WordprocessingDocumentType.Document);

            // Add a MainDocumentPart object.
            MainDocumentPart mainDocumentPart = 
            // Creatre the document structure.
            mainDocumentPart.Document = new Document();
            Document document = mainDocumentPart.Document;
            Body body = document.AppendChild(new Body());
            Paragraph para = body.AppendChild(new Paragraph());
            Run run = para.AppendChild(new Run());
            // Write some text to the file.
            run.AppendChild(new Text("Hello, World!"));

            // Close the document.

            Console.WriteLine("The document has been created.\nPress a key");

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