2011-10-15 60 views
0

我正在使用Xelement-Linq to XML来解析一些RSS提要。Linq XML如何忽略html代码?

RSS例:

<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> 

其所有工作正常,但描述元素有很多的HTML标记,我需要删除。

说明:

<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> 

谁能帮助呢?

+0

你是什么意思“忽略html代码”。你想提取文本? – adatapost

+0

@AVD是的,我只想提取文本,并忽略标记。 – Rhys

+1

看看这个链接 - http://www.dotnetperls.com/remove-html-tags – adatapost

回答

3

如果它是一个RSSFeed你为什么不结合使用System.ServiceModel.Syndication的SyncicationFeed用XML阅读器将处理您的XmlEncoded发出

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

然后删除HTML标签用正则表达式为建议由@nemesv,或使用类似这样的东西

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