CompositeNodeRemoveSmartTags Method

Removes all SmartTag descendant nodes of the current node.

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

Syntax

public void RemoveSmartTags()

Remarks

This method does not remove the content of the smart tags.

Examples

Removes all smart tags from descendant nodes of the composite node.
Document doc = new Document(MyDir + "Smart tags.doc");
Assert.AreEqual(8, doc.GetChildNodes(NodeType.SmartTag, true).Count);

// Remove smart tags from the whole document
doc.RemoveSmartTags();

Assert.AreEqual(0, doc.GetChildNodes(NodeType.SmartTag, true).Count);
Shows how to create smart tags.
public void Create()
{
    Document doc = new Document();
    SmartTag smartTag = new SmartTag(doc);
    smartTag.Element = "date";

    // Specify a date and set smart tag properties accordingly
    smartTag.AppendChild(new Run(doc, "May 29, 2019"));

    smartTag.Properties.Add(new CustomXmlProperty("Day", string.Empty, "29"));
    smartTag.Properties.Add(new CustomXmlProperty("Month", string.Empty, "5"));
    smartTag.Properties.Add(new CustomXmlProperty("Year", string.Empty, "2019"));

    // Set the smart tag's uri to the default
    smartTag.Uri = "urn:schemas-microsoft-com:office:smarttags";

    doc.FirstSection.Body.FirstParagraph.AppendChild(smartTag);
    doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, " is a date. "));

    // Create and add one more smart tag, this time for a financial symbol
    smartTag = new SmartTag(doc);
    smartTag.Element = "stockticker";
    smartTag.Uri = "urn:schemas-microsoft-com:office:smarttags";

    smartTag.AppendChild(new Run(doc, "MSFT"));

    doc.FirstSection.Body.FirstParagraph.AppendChild(smartTag);
    doc.FirstSection.Body.FirstParagraph.AppendChild(new Run(doc, " is a stock ticker."));

    // Print all the smart tags in our document with a document visitor
    doc.Accept(new SmartTagVisitor());

    // SmartTags are supported by older versions of microsoft Word
    doc.Save(ArtifactsDir + "SmartTag.Create.doc");

    // We can strip a document of all its smart tags with RemoveSmartTags()
    Assert.AreEqual(2, doc.GetChildNodes(NodeType.SmartTag, true).Count);
    doc.RemoveSmartTags();
    Assert.AreEqual(0, doc.GetChildNodes(NodeType.SmartTag, true).Count);

}

/// <summary>
/// DocumentVisitor implementation that prints smart tags and their contents
/// </summary>
private class SmartTagVisitor : DocumentVisitor
{
    /// <summary>
    /// Called when a SmartTag node is encountered in the document.
    /// </summary>
    public override VisitorAction VisitSmartTagStart(SmartTag smartTag)
    {
        Console.WriteLine($"Smart tag type: {smartTag.Element}");
        return VisitorAction.Continue;
    }

    /// <summary>
    /// Called when the visiting of a SmartTag node is ended.
    /// </summary>
    public override VisitorAction VisitSmartTagEnd(SmartTag smartTag)
    {
        Console.WriteLine($"\tContents: \"{smartTag.ToString(SaveFormat.Text)}\"");

        if (smartTag.Properties.Count == 0)
        {
            Console.WriteLine("\tContains no properties");
        }
        else
        {
            Console.Write("\tProperties: ");
            string[] properties = new string[smartTag.Properties.Count];
            int index = 0;

            foreach (CustomXmlProperty cxp in smartTag.Properties)
                properties[index++] = $"\"{cxp.Name}\" = \"{cxp.Value}\"";

            Console.WriteLine(string.Join(", ", properties));
        }

        return VisitorAction.Continue;
    }
}

See Also