从字符串中删除HTML
我试图从我的RSS源清除HTML编码。 我无法弄清楚如何设置以下来取出HTML编码。
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 = "hello there
"; var withoutHtml = Regex.Replace(withHtml, "<.+?>", string.Empty);
这将清除html只留下文本,所以“你好那里”
所以,你可以复制并使用这个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) };
使用以下类实用程序:
HttpUtility.HtmlDecode(string);
请不要再提这个答案了。