从字符串中删除 HTML [英] Remove HTML from string

查看:26
本文介绍了从字符串中删除 HTML的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试清除 RSS 源中的 HTML 编码.我不知道如何设置下面的内容来去掉 HTML 编码.

I am trying to clear the HTML coding from my RSS feed. I can not work out how to set the below to take out the HTML encoding.

var rssFeed = XElement.Parse(e.Result);

var currentFeed = this.DataContext as app.ViewModels.FeedViewModel;
var items = from item in rssFeed.Descendants("item")                            
            select new ATP_Tennis_App.ViewModels.FeedItemViewModel()
            {

                Title = item.Element("title").Value,
                DatePublished = DateTime.Parse(item.Element("pubDate").Value),
                Url = item.Element("link").Value,
                Description = item.Element("description").Value
            };

foreach (var item in items)
    currentFeed.Items.Add(item);

推荐答案

只需使用以下代码:

var withHtml = "<p>hello <b>there</b></p>";
var withoutHtml = Regex.Replace(withHtml, "<.+?>", string.Empty);

这将清理 html 只留下文本,所以你好"

This will clean the html leaving only the text, so "hello there"

所以,你可以复制并使用这个函数:

So, you can just copy and use this function:

string RemoveHtmlTags(string html) {
    return Regex.Replace(html, "<.+?>", string.Empty);
}

您的代码将如下所示:

var rssFeed = XElement.Parse(e.Result);
var currentFeed = this.DataContext as app.ViewModels.FeedViewModel;
var items = from item in rssFeed.Descendants("item")                            
            select new ATP_Tennis_App.ViewModels.FeedItemViewModel()
            {

                Title = RemoveHtmlTags(item.Element("title").Value),
                DatePublished = DateTime.Parse(item.Element("pubDate").Value),
                Url = item.Element("link").Value,
                Description = RemoveHtml(item.Element("description").Value)
            };

这篇关于从字符串中删除 HTML的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆