MailBee.NET Objects 4.0

RuleSet.AddTagProcessingRule Method 

Adds a rule for changing attributes of HTML tags.

public void AddTagProcessingRule(
   string tagName,
   TagAttributeCollection tagAttrs,
   TagAttributeCollection attrsToAdd,
   TagAttributeCollection attrsToRemove,
   bool replaceMode
);

Parameters

tagName
The name of the tag for which to change the attributes. 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).
attrsToAdd
The list of attributes to be added to the tagName tag. Can be a null reference if replaceMode is false.
attrsToRemove
The list of attributes to be removed from the tagName tag. Can be a null reference if replaceMode is false.
replaceMode
If true, attrsToRemove attributes will be replaced with attrsToAdd ones; otherwise, attrsToRemove attributes will simply be removed and attrsToAdd ones will be added.

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.

Once the matching tag was found and the delegate (if any) returned true, the processing continues as follows (depending on replaceMode value):

Note   tagAttrs collection is not related with attrsToAdd or attrsToRemove in any way. tagAttrs only specifies attributes any of which the tagName tag must have in order to satisfy the rule condition. However, tagAttrs is not used later when the rule action begins.

Exceptions

Exception TypeCondition
MailBeeInvalidArgumentException tagName is a null reference (Nothing in Visual Basic) or an empty string, or replaceMode is true and attrsToAdd does not match attrsToRemove.

Example

This sample change the background color of the HTML page to black.

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

class Sample
{
    static void Main(string[] args)
    {
        // Create new stream with an HTML markup.
        WebRequest myWebRequest = WebRequest.Create("http://www.afterlogic.com"); 
        WebResponse myWebResponse = myWebRequest.GetResponse(); 
        Stream receivingStream = myWebResponse.GetResponseStream();

        Processor htmlProcessor = new Processor();

        htmlProcessor.LoadFromStream(receivingStream, Global.DefaultEncoding);

        RuleSet rules = new RuleSet();

        // Create new rule for processing.
        TagAttributeCollection attributes = new TagAttributeCollection();
        TagAttribute attribute = new TagAttribute();
        attribute.Definition = "bgcolor = 'black'";

        attributes.Add(attribute);

        rules.AddTagRemovalRule("link", null);
        rules.AddTagProcessingRule("body", null, attributes, null, false);

        // Processing of the rule.
        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 System.IO
Imports System.Net
Imports MailBee
Imports MailBee.Html

Module Sample
    Sub Main()
        ' Create new stream with an HTML markup.
        Dim myWebRequest As WebRequest = WebRequest.Create("http://www.afterlogic.com")
        Dim myWebResponse As WebResponse = myWebRequest.GetResponse()
        Dim receivingStream As Stream = myWebResponse.GetResponseStream()

        Dim htmlProcessor As New Processor

        htmlProcessor.LoadFromStream(receivingStream, Global.DefaultEncoding)

        Dim rules As New RuleSet

        ' Create new rule for processing.
        Dim attributes As TagAttributeCollection = New TagAttributeCollection
        Dim attribute As TagAttribute = New TagAttribute
        attribute.Definition = "bgcolor = 'black'"

        attributes.Add(attribute)

        rules.AddTagRemovalRule("link", Nothing)
        rules.AddTagProcessingRule("body", Nothing, attributes, Nothing, False)

        ' Processing of the rule.
        Console.WriteLine(htmlProcessor.Dom.ProcessToString(rules, Nothing))
    End Sub
End Module

See Also

RuleSet Class | MailBee.Html Namespace