I need to encode all the text, leaving <and> untouched.
Example
<p>Give me 100.000 €!</p>
should become:
<p>Give me 100.000 €!</p>
html tags should remain intact
you can go html agility pack and then encode tag values
Maybe use string.replace only for those characters that you want to encode?
, , , :
html = Regex.Replace( html, "(<[^>]+>|[^<]+)", m => m.Value.StartsWith("<") ? m.Value : HttpUtility.HtmlEncode(m.Value) );
htmlencode HtmlTextWriter. HtmlTextWriter <p></p>, <p></p> HtmlEncode. HtmlTextWriter ToString(); , .
<p></p>
, 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 , .
Source: https://habr.com/ru/post/1732356/More articles:IValueCOnverter not working - c #Как считать обе стороны отношений "многие ко многим" в Google App Engine - pythonA non-stopping example of WSGI (practical approach to WSGI) - pythonRight alignment and vertical alignment with CSS checkbox / radio button - htmlIs there a list of the most common English words for indexing text to search? - searchWCF Counters in perfmon.exe - wcfDojo Filtering Select: Unable to add parameters - dojoCocoa: How to make a small toolbar, for example, in Pages or Numbers? - xcodeС++ фатальная ошибка c1083 проект был прекрасен раньше, что теперь? - c++https://translate.googleusercontent.com/translate_c?depth=1&pto=aue&rurl=translate.google.com&sl=ru&sp=nmt4&tl=en&u=https://fooobar.com/questions/1732361/sap-making-https-requests-to-rest-service&usg=ALkJrhgieX67WpH2cACzLCfaerS9CfxRvwAll Articles