MailBee.NET Objects 7.1

RuleSet.AddTagReplacementRule Method (String, TagAttributeCollection, String, Boolean)

Adds a rule for replacing certain tags with the specified replacement string.

public void AddTagReplacementRule(
   string tagName,
   TagAttributeCollection tagAttrs,
   string replacement,
   bool replaceTagDefinitionOnly
);

Parameters

tagName
The name of the tag to be replaced. Case-insensitive.
tagAttrs
The list of attributes any of which must exist in the definition of tagName tags in order to trigger the rule's action, or a null reference (Nothing in Visual Basic) to apply the rule for any tags with tagName name (regardless of which attributes they have).
replacement
The string which will replace TagDefinition or OuterHtml of the the original element.
replaceTagDefinitionOnly
If true, only the tag definition (opening tag and closing tag if any) will be replaced; otherwise, the entire Element with all inner contents will be replaced.

Remarks

The condition when this rule satisfies is the same as for ProcessingCondition rule (created with AddTagProcessingCondition method). See AddTagProcessingCondition topic for the details which apply to the HTML processing rules of all types.

The action for this rule replaces the matching tag either completely (OuterHtml gets replaced) or partially (only TagDefinition gets replaced) depending on replaceTagDefinitionOnly value. If replaceTagDefinitionOnly is true, this rule simply renames the matching tags removing the original name and attributes with supplied ones while leaving InnerHtml intact.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentException tagName is a null reference (Nothing in Visual Basic) or an empty string, or replacement is a null reference (or an empty string if replaceTagDefinitionOnly is true).

Example

This sample replaces BR tag definitions with HR tag definitions.

[C#]
// To use the code below, import these namespaces at the top of your code.
using System;
using MailBee.Mime;
using MailBee.Html;

class Sample
{
    static void Main(string[] args)
    {
        // Load HTML message from file.
        MailMessage message = new MailMessage();
        message.LoadMessage(@"C:\message.eml");

        Processor htmlProcessor = new Processor();

        htmlProcessor.Dom.OuterHtml = message.BodyHtmlText;

        RuleSet rules = new RuleSet();

        rules.AddTagReplacementRule("br", null, "hr", true);

        htmlProcessor.Dom.Process(rules, null);

        // Process the rule and display the results.
        Console.WriteLine(htmlProcessor.Dom.ProcessToString(rules, null));
    }
}
[Visual Basic]
' To use the code below, import these namespaces at the top of your code.
Imports System
Imports MailBee.Mime
Imports MailBee.Html

Module Sample
    Sub Main()
        ' Load HTML message from file.
        Dim message As New MailMessage
        message.LoadMessage("C:\message.eml")

        Dim htmlProcessor As New Processor

        htmlProcessor.Dom.OuterHtml = message.BodyHtmlText

        Dim rules As New RuleSet

        rules.AddTagReplacementRule("br", Nothing, "hr", True)

        htmlProcessor.Dom.Process(rules, Nothing)

        ' Process the rule and display the results.
        Console.WriteLine(htmlProcessor.Dom.ProcessToString(rules, Nothing))
    End Sub
End Module

See Also

RuleSet Class | MailBee.Html Namespace | RuleSet.AddTagReplacementRule Overload List