编码的HTML要解码,但它的格式不正确
本文关键字:格式 不正确 解码 HTML 编码 | 更新日期: 2023-09-27 18:05:37
我已经从数据库中获取了一些编码的html。而获取i解码的html在某个地方。但是粗体/斜体&其他格式不显示,只显示文本。
我的代码 string a = da.GetLeftPanelData();//<-- in here Encoded html comes like <p style="text-align: justify;">
string b = WebUtility.HtmlDecode(a);
mypanel.InnerText = Regex.Replace(b, @"<[^>]+>| ", "").Trim();
您的Regex.Replace
调用剥离了所有的HTML标记,因此,所有的格式化信息。
精化
假设从数据库中读取的字符串a
为:
<p>Text can be <b>bold</b> or <i>italic</i>.</p>
则解码后的字符串b
为:
<p>Text can be <b>bold</b> or <i>italic</i>.</p>
regex模式匹配每一个出现的<
,后面跟着一些字符,后面跟着>
。因此,Regex.Replace
调用替换每个HTML标签(例如。<p>
, <b>
, </i>
), myPanel.InnerText
变为:
Text can be bold or italic.