Encode html in Asp.net C # but leave tags intact

I need to encode all the text, leaving <and> untouched.

Example

<p>Give me 100.000 €!</p>

should become:

<p>Give me 100.000 &euro;!</p>

html tags should remain intact

+3
source share
5 answers

you can go html agility pack and then encode tag values

+2
source

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

+1
source

, , , :

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

htmlencode HtmlTextWriter. HtmlTextWriter <p></p>, <p></p> HtmlEncode. HtmlTextWriter ToString(); , .

0

, HtmlAgilityPack.

 public static class HtmlTextEncoder
 {
    public static string HtmlEncode(string html)
    {
        if (html == null) return null;

        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;
        }
    }

    private static void EncodeNode(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);
                }
            }
        }
        else if (node.NodeType == HtmlNodeType.Text)
        {
            node.InnerHtml = HttpUtility.HtmlEncode(node.InnerHtml);
        }
    }
}

HTML HTML- .

.NET , .

0

Source: https://habr.com/ru/post/1732356/


All Articles