The NullUserException answer is perfect, I made a small extension method to do this, and I am posting here if anyone else needs it.
using System; using System.Collections.Generic; using System.Linq; using System.Text; using System.Xml; using System.IO; namespace Extenders { public static class StringExtender { internal static void ParseHtmlDocument(XmlDocument doc, XmlNode root, string[] allowedTags, string[] allowedAttributes, string[] allowedStyleKeys) { XmlNodeList nodes; if (root == null) root = doc.ChildNodes[0]; nodes = root.ChildNodes; foreach (XmlNode node in nodes) { if (!(allowedTags.Any(x => x.ToLower() == node.Name.ToLower()))) { var safeNode = doc.CreateTextNode(node.InnerText); root.ReplaceChild(safeNode, node); } else { if (node.Attributes != null) { var attrList = node.Attributes.OfType<XmlAttribute>().ToList(); foreach (XmlAttribute attr in attrList) { if (!(allowedAttributes.Any(x => x.ToLower() == attr.Name))) { node.Attributes.Remove(attr); }
For use:
var x = "<b>allowed</b><b class='text'>allowed attr</b><b id='5'>not allowed attr</b><i>not all<b>o</b>wed tag</i>".ParseSafeHtml((new string[] { "b", "#text" }), (new string[] { "class" }), (new string[] { }));
What outputs:
<b>allowed</b><b class='text'>allowed attr</b><b>not allowed attr</b>not allowed tag
If the item is not resolved, it will receive an innerText and pull out the tag, removing all the internal tags.
Brunolm
source share