NodeToString(SaveFormat) Method

Exports the content of the node into a string in the specified format.

Namespace:  Aspose.Words
Assembly:  Aspose.Words (in Aspose.Words.dll) Version: 20.7.0


public string ToString(
	SaveFormat saveFormat


Type: Aspose.WordsSaveFormat

Return Value

Type: String
The content of the node in the specified format.


Shows the difference between calling the GetText and ToString methods on a node.
Document doc = new Document();

// Enter a field into the document
DocumentBuilder builder = new DocumentBuilder(doc);
builder.InsertField("MERGEFIELD Field");

// GetText will retrieve all field codes and special characters
Assert.AreEqual("\u0013MERGEFIELD Field\u0014«Field»\u0015\u000c", doc.GetText());

// ToString will give us the plaintext version of the document in the save format we put into the parameter
Assert.AreEqual("«Field»\r\n", doc.ToString(SaveFormat.Text));
Exports the content of a node to String in HTML format.
Document doc = new Document(MyDir + "Document.docx");

// Extract the last paragraph in the document to convert to HTML
Node node = doc.LastSection.Body.LastParagraph;

// When ToString is called using the html SaveFormat overload then the node is converted directly to html
Assert.AreEqual("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%; font-size:12pt\">" +
                "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" +
                "</p>", node.ToString(SaveFormat.Html));

// We can also modify the result of this conversion using a SaveOptions object
HtmlSaveOptions saveOptions = new HtmlSaveOptions();
saveOptions.ExportRelativeFontSize = true;

Assert.AreEqual("<p style=\"margin-top:0pt; margin-bottom:8pt; line-height:108%\">" +
                "<span style=\"font-family:'Times New Roman'\">Hello World!</span>" +
                "</p>", node.ToString(saveOptions));
Shows how to extract the label of each paragraph in a list as a value or a String.
Document doc = new Document(MyDir + "Rendering.docx");

NodeCollection paras = doc.GetChildNodes(NodeType.Paragraph, true);

// Find if we have the paragraph list. In our document our list uses plain arabic numbers,
// which start at three and ends at six
foreach (Paragraph paragraph in paras.OfType<Paragraph>().Where(p => p.ListFormat.IsListItem))
    Console.WriteLine($"List item paragraph #{paras.IndexOf(paragraph)}");

    // This is the text we get when actually getting when we output this node to text format
    // The list labels are not included in this text output. Trim any paragraph formatting characters
    string paragraphText = paragraph.ToString(SaveFormat.Text).Trim();
    Console.WriteLine($"\tExported Text: {paragraphText}");

    ListLabel label = paragraph.ListLabel;
    // This gets the position of the paragraph in current level of the list. If we have a list with multiple level then this
    // will tell us what position it is on that particular level
    Console.WriteLine($"\tNumerical Id: {label.LabelValue}");

    // Combine them together to include the list label with the text in the output
    Console.WriteLine($"\tList label combined with text: {label.LabelString} {paragraphText}");

See Also