0

使用 rss feed(syndicationfeed) 我有一些编码文本,通常在视图中我可以说 @Html.Raw(feed.summary) 其中 feed.summary 是一些带有文本的 html,它会显示没有所有文本的文本html代码。但是由于某种原因,这不起作用。它打破了我的看法,没有崩溃,只是没有显示任何东西。所以我想知道是否有一种方法可以在代码中执行此操作,这样文本将全部准备好,并且在我看来我不必使用 Html.Raw() 。任何建议表示赞赏。

 @Html.Raw(feed.RssShortSummary) 
  //This doesn't work, it messes all my styling up and simply doesn't even display the text.

在后面的代码中:(我尝试过 MvcHtmlString.Create())但仍然返回 html 代码,而不是我需要的文本。

   MvcHtmlString.Create(summary);

所以如果我有:

 <p>Here is some text</p> 

Html.Raw() 将返回“Here is some text”,但由于某种原因这会扰乱我的观点。我已经尝试过 MvcHtmlString.Create 但它仍然返回给我:

<p>Here is some text</p> //returning all the html instead just "Here is some text"

在代码c#中我试过:

  var x = MvcHtmlString.Create(rssItem.RssSummary);

例如 rssItem.RssSummary =

  <p>Here is some text</p>

x 仍在生成:

     <p>Here is some text</p> 
4

1 回答 1

2

在 .net 中有许多删除标签的方法,C# Remove HTML Tags不错的文章,其中描述了几种删除标签的方法,基于该文章,删除标签的最佳方法是 StripTagsCharArray,因为它比 Regex 方法更快:

    /// <summary>
    /// Remove HTML tags from string using char array.
    /// </summary>
    public static string StripTagsCharArray(string source)
    {
    char[] array = new char[source.Length];
    int arrayIndex = 0;
    bool inside = false;

    for (int i = 0; i < source.Length; i++)
    {
        char let = source[i];
        if (let == '<')
        {
        inside = true;
        continue;
        }
        if (let == '>')
        {
        inside = false;
        continue;
        }
        if (!inside)
        {
        array[arrayIndex] = let;
        arrayIndex++;
        }
    }
    return new string(array, 0, arrayIndex);
    }
于 2012-11-28T06:24:58.733 回答