Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- namespace ZetaProducer.RuntimeBusinessLogic.Webbing.Printing
- {
- using System;
- using System.Linq;
- using HtmlAgilityPack;
- internal static class PrintAgilityPackExtensions
- {
- public static void RemoveElementsByClass(this HtmlDocument doc, string className)
- {
- var nodes = doc.DocumentNode.SelectNodes(string.Format(@"//*[contains(@class,'{0}')]", className));
- if (nodes != null)
- {
- foreach (var node in nodes)
- {
- if (node != null)
- {
- // Muss ganze Klasse haben.
- var classes = node.Attributes[@"class"].Value;
- if (containsFullWord(classes, className))
- {
- var parent = node.ParentNode;
- if (parent != null)
- {
- parent.RemoveChild(node);
- }
- }
- }
- }
- }
- }
- private static bool containsFullWord(string classes, string className)
- {
- if (string.IsNullOrWhiteSpace(classes) || string.IsNullOrWhiteSpace(className))
- {
- return false;
- }
- else
- {
- classes = classes.Trim();
- className = className.Trim();
- var raw = classes.Split(new[] { ' ' }, StringSplitOptions.RemoveEmptyEntries);
- return raw.Any(s => string.Equals(s, className));
- }
- }
- public static void RemoveElementsByID(this HtmlDocument doc, string id)
- {
- var nodes = doc.DocumentNode.SelectNodes(string.Format(@"//*[@id='{0}']", id));
- if (nodes != null)
- {
- foreach (var node in nodes)
- {
- var parent = node.ParentNode;
- if (parent != null)
- {
- parent.RemoveChild(node);
- }
- }
- }
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement