0

I am using Xelement - Linq to XML to parse some an RSS feed.

Rss Example:

    <item>
      <title>Waterfront Ice Skating</title>
      <link>http://www.eventfinder.co.nz/2011/sep/wellington/wellington-waterfront-ice-skating?utm_medium=rss</link>
      <description>&lt;p&gt;An ice skating rink in Wellington for a limited time only! 

Enjoy the magic of the New Zealand winter at an outdoor skating experience with all the fun and atmosphere of New York&amp;#039;s Rockefeller Centre or Central Park, ...&lt;/p&gt;&lt;p&gt;Wellington | Friday, 30 September 2011 - Sunday, 30 October 2011&lt;/p&gt;</description>
      <content:encoded><![CDATA[Today, Wellington Waterfront<br/>Wellington]]></content:encoded>
      <guid isPermalink="false">108703</guid>
      <pubDate>2011-09-30T10:00:00Z</pubDate>
      <enclosure url="http://s1.eventfinder.co.nz/uploads/events/transformed/190501-108703-13.jpg" length="5000" type="image/jpeg"></enclosure>
    </item>

Its all working fine but the description element has alot of html markup that I need to remove.

Description:

<description>&lt;p&gt;An ice skating rink in Wellington for a limited time only! 

    Enjoy the magic of the New Zealand winter at an outdoor skating experience with all the fun and atmosphere of New York&amp;#039;s Rockefeller Centre or Central Park, ...&lt;/p&gt;&lt;p&gt;Wellington | Friday, 30 September 2011 - Sunday, 30 October 2011&lt;/p&gt;</description>

Could anyone assist with this?

KV Prajapati
  • 93,659
  • 19
  • 148
  • 186
Rhys
  • 2,807
  • 8
  • 46
  • 68

2 Answers2

3

If it is a RSSFeed why don't you use System.ServiceModel.Syndication, the SyncicationFeed in combination with a XML reader will deal with your XmlEncoded issues

            using (XmlReader reader = XmlReader.Create(@"C:\\Users\\justMe\\myXml.xml"))
            {
                SyndicationFeed myFeed = SyndicationFeed.Load(reader);
                ...
            }

Then remove HTML-Tags with regex as suggested by @nemesv, or use something like this

    public static string StripHTML(this string htmlText)
    {
        var reg = new Regex("<[^>]+>", RegexOptions.IgnoreCase);
        return HttpUtility.HtmlDecode(reg.Replace(htmlText, string.Empty));
    }
tazyDevel
  • 436
  • 3
  • 12
1

First you should HtmlDecode the content of the descirptoin with System.Net.HttpUtility.HtmlDecode. This replaces the encoded &lt ;p&gt ; to <p> and then you can remove the HTML tags with regex: Using C# regular expressions to remove HTML tags or with some other HTML parsing library.

Community
  • 1
  • 1
nemesv
  • 138,284
  • 16
  • 416
  • 359
  • 1
    No, it is XmlEncoded, not HtmlEncoded. Just getting XElement.Value will do, HtmlDecode could go wrong. – H H Oct 15 '11 at 09:03