簡體   English   中英

從字符串中刪除 html 標記

[英]Remove html markup from string

我正在從數據庫中讀取一個字段並顯示在 GridView 中,並且在該字段中它包含文本中的<br/>標記。 所以我試圖從代碼中刪除這些,但是當我檢查e.Row.Cells[index].Text的值時,它不包含<br/>並且有;br/&gt; 反而。

因此,我嘗試創建一個 function,它刪除任何以<開頭並以>結尾或以&開頭並以;結尾的 substring。 . 代碼刪除了<>但它仍然顯示br/

代碼:

index = gv.Columns.HeaderIndex("Message");
if (index > 0)
{
   string message = RemoveHTMLMarkup(e.Row.Cells[index].Text);
   e.Row.Cells[index].Text = message;
}

static string RemoveHTMLMarkup(string text)
{
        return Regex.Replace(Regex.Replace(text, "<.+?>", string.Empty), "&.+?;", string.Empty);
}

如何刪除<br/>標簽?

由於這是文字字符串,因此您(sh | c)僅使用String.Replace()

static string RemoveHTMLNewLines(string text)
{
    return text.Replace("&lt;br/&gt;", string.Empty);
}

或根據需要替換為Environment.NewLine

  1. 取消激活字符串。
  2. 然后使用正則表達式來查找和刪除預期的標簽。

要么

如果您有足夠的時間學習和使用,請使用HtmlAgilityPack軟件包。

關於HtmlAgilityPack

Nuget軟件包鏈接

不管問題如何,我很好奇這是什么:

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM