无法将HTML转换为纯文本:ASP.net

时间:2016-12-11 13:11:45

标签: c# html asp.net .net

我有从sql server检索到的HTML文本。

"<span style="color: rgb(36, 39, 41); font-family: Arial, &quot;Helvetica Neue&quot;, 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的标签上。

要执行此操作:

  1. 我已从Dataset中的sql server检索数据。
  2. 将检索上方设置为标签

         <h3 id="lblqNotes" runat="server" class="text-questiondata" style="color:#1E90FF">
       </h3>
    
  3. 代码背后:

      lblqNotes.InnerText = System.Net.WebUtility.HtmlDecode(values[2]);
    

    但标签上的输出仍然是HTML代码,而不是纯文本。

    感谢您的帮助。

2 个答案:

答案 0 :(得分:2)

您可以使用XmlDocument

来实现此目的
    static void Main(string[] args)
    {
        var xmlDocument = new XmlDocument();
        var html = @"<span style=""color: rgb(36, 39, 41); font - family: Arial, &quot; Helvetica Neue&quot;, 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);

删除Html