RuleSetAddTagProcessingCondition Method |
Namespace: MailBee.Html
Exception | Condition |
---|---|
MailBeeInvalidArgumentException | tagName is a null reference (Nothing in Visual Basic) or an empty string. |
For instance, if tagName is A and tagAttrs is a null reference, Process(RuleSet, ProcessElementDelegate) method will execute ProcessElementDelegate delegate (specified in del parameter) for all <A> tags in the document.
If tagName is INPUT and tagAttrs contains TYPE=IMAGE and TYPE=FILE attributes, Process(RuleSet, ProcessElementDelegate) method will execute the delegate for all <INPUT TYPE=IMAGE> and <INPUT TYPE=FILE> tags but not for <INPUT TYPE=TEXT> tags.
This method creates a Rule of ProcessingCondition type. Rules of other types have the same conditions (tag name and optional list of attributes). The only difference between rule types is what action to perform when the condition gets satisfied. ProcessingCondition rule simply calls the delegate. Other rules call the delegate first, then check the return value of the delegate and perform the specified action only if the delegate returned true.
Note |
---|
If Process(RuleSet, ProcessElementDelegate) method was called with del parameter set to a null reference, ProcessingCondition rule will have no effect. Rules of other types will be executed as if the delegate always returned true. |
The above applies to both Process(RuleSet, ProcessElementDelegate) and ProcessToString(RuleSet, ProcessElementDelegate) methods.
// To use the code below, import these namespaces at the top of your code. using System.IO; using System.Net; using MailBee; using MailBee.Html; class Sample { // The delegate is used for processing of the HTML source. // All IMG tags will be removed and all BR tags will be replaced with HR tags. static bool ProcessDelegate(Element elem, Rule rule) { switch (elem.TagName.ToLower()) { case "img": elem.Remove(); break; case "br": elem.TagName = "hr"; return false; } return true; } static void Main(string[] args) { // Create a new stream and load the HTML document into it. WebRequest myWebRequest = WebRequest.Create("http://www.afterlogic.com"); WebResponse myWebResponse = myWebRequest.GetResponse(); Stream receivingStream = myWebResponse.GetResponseStream(); Processor htmlProcessor = new Processor(); // Load the stream into Processor instance. htmlProcessor.LoadFromStream(receivingStream, Global.DefaultEncoding); ProcessElementDelegate del = new ProcessElementDelegate(ProcessDelegate); RuleSet rules = new RuleSet(); // Add HTML processing rules. rules.AddTagProcessingCondition("img", null); rules.AddTagProcessingCondition("br", null); // Process the delegate and the rules. htmlProcessor.Dom.Process(rules, del); // Save the resulting HTML to disk. FileStream outStream = new FileStream(@"C:\Temp\afterlogic.html", FileMode.Create, FileAccess.Write, FileShare.None); htmlProcessor.SaveToStream(outStream, Global.DefaultEncoding); outStream.Close(); } }