RuleSetAddTagProcessingRule Method
Adds a rule for changing attributes of HTML tags.

Namespace: MailBee.Html
Assembly: MailBee.NET (in MailBee.NET.dll) Version: 11.2.0 build 590 for .NET 4.5
Syntax
public void AddTagProcessingRule(
	string tagName,
	TagAttributeCollection tagAttrs,
	TagAttributeCollection attrsToAdd,
	TagAttributeCollection attrsToRemove,
	bool replaceMode
)

Parameters

tagName
Type: SystemString
The name of the tag for which to change the attributes. Case-insensitive.
tagAttrs
Type: MailBee.HtmlTagAttributeCollection
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
Type: MailBee.HtmlTagAttributeCollection
The list of attributes to be added to the tagName tag. Can be a null reference if replaceMode is false.
attrsToRemove
Type: MailBee.HtmlTagAttributeCollection
The list of attributes to be removed from the tagName tag. Can be a null reference if replaceMode is false.
replaceMode
Type: SystemBoolean
If true, attrsToRemove attributes will be replaced with attrsToAdd ones; otherwise, attrsToRemove attributes will simply be removed and attrsToAdd ones will be added.
Exceptions
ExceptionCondition
MailBeeInvalidArgumentExceptiontagName is a null reference (Nothing in Visual Basic) or an empty string, or replaceMode is true and attrsToAdd does not match attrsToRemove.
Remarks

The condition when this rule satisfies is the same as for ProcessingCondition rule (created with AddTagProcessingCondition(String, TagAttributeCollection) method). See AddTagProcessingCondition(String, TagAttributeCollection) 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):

  • If replaceMode is false, MailBee searches for any attrsToRemove attributes in the tag definition and removes them. attrsToAdd attributes are added. Any of these actions are optional (and thus attrsToAdd or attrsToRemove can be empty).
  • If replaceMode is true, MailBee compares lengths of attrsToAdd and attrsToRemove collections. If they are equal, MailBee will then iterate through every attribute of the tag and if it finds such attribute in attrsToRemove (let's say, at index i), it replaces i-th attribute in attrsToRemove with i-th attribute in attrsToAdd. In other words, attributes A, B and C will be replaced with X, Y and Z correspondingly. If lengths of attrsToAdd and attrsToRemove are not equal but attrsToAdd length is 1, any attributes contained in attrsToRemove will be replaced with the attribute in attrsToAdd. In other words, attributes A, B and C will be replaced with X, X and X correspondingly. Otherwise (attrsToAdd and/or attrsToRemove is a null reference or their lengths do not match), MailBee will throw an exception.
Note 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.
Examples
This sample change the background color of the HTML page to black.
// 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));
    }
}
See Also