Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Remove HTML from string

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);
like image 496
Michael Peberdy Avatar asked Apr 01 '12 19:04

Michael Peberdy


People also ask

How do I strip a string in HTML?

To strip out all the HTML tags from a string there are lots of procedures in JavaScript. In order to strip out tags we can use replace() function and can also use . textContent property, . innerText property from HTML DOM.

How do you remove HTML?

Select the HTML element which need to remove. Use JavaScript remove() and removeChild() method to remove the element from the HTML document.

Is it possible to remove the HTML tags from data?

PHP provides an inbuilt function to remove the HTML tags from the data. The strip_tags() function is an inbuilt function in PHP that removes the strings form HTML, XML and PHP tags. It accepts two parameters. This function returns a string with all NULL bytes, HTML, and PHP tags stripped from a given $str.


1 Answers

Just use the following code:

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

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);
}

Your code will look something like this:

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)
            };
like image 95
Pedro Lamas Avatar answered Sep 19 '22 07:09

Pedro Lamas