Skip to content Skip to sidebar Skip to footer

Encode Html In Asp.net C# But Leave Tags Intact

I need to encode a whole text while leaving the < and > intact. example

Give me 100.000 €!

must become:

Give me 100.000 €!

Solution 1:

Use a regular expression that matches either a tag or what's between tags, and encode what's between:

html = Regex.Replace(
  html,
  "(<[^>]+>|[^<]+)",
  m => m.Value.StartsWith("<") ? m.Value : HttpUtility.HtmlEncode(m.Value)
);

Solution 2:

Maybe use string.replace for just those characters you want to encode?

Solution 3:

You could use HtmlTextWriter in addition to htmlencode. So you would use HtmlTextWriter to setup your <p></p> and then just set the body of the <p></p> using HtmlEncode. HtmlTextWriter allow ToString(); and a bunch of other methods so it shouldn't be much more code.

Solution 4:

As others have suggested, this can be achieved with HtmlAgilityPack.

publicstaticclassHtmlTextEncoder
 {
    publicstaticstringHtmlEncode(string html)
    {
        if (html == null) returnnull;

        var doc = new HtmlDocument();
        doc.LoadHtml(html);

        EncodeNode(doc.DocumentNode);

        doc.OptionWriteEmptyNodes = true;
        using (var s = new MemoryStream())
        {
            doc.Save(s);
            var encoded = doc.Encoding.GetString(s.ToArray());
            return encoded;
        }
    }

    privatestaticvoidEncodeNode(HtmlNode node)
    {
        if (node.HasChildNodes)
        {
            foreach (var childNode in node.ChildNodes)
            {
                if (childNode.NodeType == HtmlNodeType.Text)
                {
                    childNode.InnerHtml = HttpUtility.HtmlEncode(childNode.InnerHtml);
                }
                else
                {
                    EncodeNode(childNode);
                }
            }
        }
        elseif (node.NodeType == HtmlNodeType.Text)
        {
            node.InnerHtml = HttpUtility.HtmlEncode(node.InnerHtml);
        }
    }
}

This iterates through all the nodes in the HTML, and replaces any text nodes with HTML encoded text.

I've created a .NET fiddle to demonstrate this technique.

Post a Comment for "Encode Html In Asp.net C# But Leave Tags Intact"