我有从sql server检索到的HTML文本。
"<span style="color: rgb(36, 39, 41); font-family: Arial, "Helvetica Neue", Helvetica, sans-serif; font-size: 15px; background-color: rgb(255, 255, 255);">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>"
我想将html转换为纯文本并将其设置在asp.net的标签上。
要执行此操作:
将检索上方设置为标签
<h3 id="lblqNotes" runat="server" class="text-questiondata" style="color:#1E90FF">
</h3>
代码背后:
lblqNotes.InnerText = System.Net.WebUtility.HtmlDecode(values[2]);
但标签上的输出仍然是HTML代码,而不是纯文本。
感谢您的帮助。
答案 0 :(得分:2)
您可以使用XmlDocument
:
static void Main(string[] args)
{
var xmlDocument = new XmlDocument();
var html = @"<span style=""color: rgb(36, 39, 41); font - family: Arial, " Helvetica Neue", Helvetica, sans - serif; font - size: 15px; background - color: rgb(255, 255, 255); "">So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.</span>";
xmlDocument.LoadXml(html);
var text = xmlDocument.InnerText;
// So, first method looks nice and it's easy but is there is any simpler way to write the second function? Both are working well and giving a correct output.
}
答案 1 :(得分:0)
尝试使用正则表达式output = Regex.Replace(source, "<[^>]*>", string.Empty);