解析XML时遇到问题?

时间:2011-04-21 05:11:38

标签: c# xml rss

我正在用C#制作一个简单的RSS阅读器应用程序。这是我第一次使用XML并需要一些帮助来解析各种rss feed中使用的不同样式。

E.g

以下是我期望的Feed类型并获得正确的结果:

<item>
      <title>Cometh the hour, cometh the man</title>
      <link>http://www.espnstar.com/rss-feed/detail/item612327</link>
      <description>Real Madrid have finally won their first trophy since 2008. Unsurprisingly, it has coincided with the arrival of one man.</description>
      <pubDate>Thu, 21 Apr 2011 04:11:42 GMT</pubDate>
    </item>

但是像这样的饲料:

    <item><title>NBA: San Antonio Spurs rally to level up with Memphis Grizzlies</title>
<link>http://timesofindia.feedsportal.com/c/33039/f/533921/s/1455e7bf/l/0Ltimesofindia0Bindiatimes0N0Csports0Cnba0Ctop0Estories0CNBA0ESan0EAntonio0ESpurs0Erally0Eto0Elevel0Eup0Ewith0EMemphis0EGrizzlies0Carticleshow0C80A443210Bcms/story01.htm</link>
<description>The San Antonio Spurs, trailed by three points at half-time, rallied to level their first round playoff series with the Memphis Grizzlies at 1-1 with a 93-87 victory.&lt;img width='1' height='1' src='http://timesofindia.feedsportal.com/c/33039/f/533921/s/1455e7bf/mf.gif' border='0'/&gt;&lt;div class='mf-viral'&gt;&lt;table border='0'&gt;&lt;tr&gt;&lt;td valign='middle'&gt;&lt;a href="http://res.feedsportal.com/viral/sendemail2.html?title=NBA%3A+San+Antonio+Spurs+rally+to+level+up+with+Memphis+Grizzlies&amp;link=http%3A%2F%2Ftimesofindia.indiatimes.com%2Fsports%2Fnba%2Ftop-stories%2FNBA-San-Antonio-Spurs-rally-to-level-up-with-Memphis-Grizzlies%2Farticleshow%2F8044321.cms" target="_blank"&gt;&lt;img src="http://res3.feedsportal.com/images/emailthis2.gif" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;td valign='middle'&gt;&lt;a href="http://res.feedsportal.com/viral/bookmark.cfm?title=NBA%3A+San+Antonio+Spurs+rally+to+level+up+with+Memphis+Grizzlies&amp;link=http%3A%2F%2Ftimesofindia.indiatimes.com%2Fsports%2Fnba%2Ftop-stories%2FNBA-San-Antonio-Spurs-rally-to-level-up-with-Memphis-Grizzlies%2Farticleshow%2F8044321.cms" target="_blank"&gt;&lt;img src="http://res3.feedsportal.com/images/bookmark.gif" border="0" /&gt;&lt;/a&gt;&lt;/td&gt;&lt;/tr&gt;&lt;/table&gt;&lt;/div&gt;&lt;br/&gt;&lt;br/&gt;&lt;a href="http://da.feedsportal.com/r/100752217265/u/242/f/533921/c/33039/s/1455e7bf/a2.htm"&gt;&lt;img src="http://da.feedsportal.com/r/100752217265/u/242/f/533921/c/33039/s/1455e7bf/a2.img" border="0"/&gt;&lt;/a&gt;</description>
<pubDate>Thu, 21 Apr 2011 04:33:44 GMT</pubDate>
</item>

如何从描述节点中提取主要描述文本,而不是像href那样提取其他内容。

如何在以下Feed中处理cdata:

<item>
<title><![CDATA[Japan declares no-go zone around nuclear plant ]]></title>

<author><![CDATA[AP]]></author>
<category><![CDATA[International]]></category>
<link>http://www.thehindu.com/news/international/article1714401.ece</link>
<description><![CDATA[
Japan declared a 20 km evacuation zone around its tsunami-crippled nuclear power plant a no-go zone on Thursday, urging residents to abide by the order for the sake of their own safety. Chi...
]]>
</description>
<pubDate><![CDATA[Thu, 21 Apr 2011 08:15:48 +0530]]></pubDate>
</item>
<item>

2 个答案:

答案 0 :(得分:2)

好吧,你可以使用正则表达式来摆脱你不需要的一切。

XElement d = XElement.Parse(feed);  // load the feed in an XElement

// get the title, link and description
string title = d.Elements("title").FirstOrDefault().Value;
string link = d.Elements("link").FirstOrDefault().Value;
string description = d.Elements("description").FirstOrDefault().Value;

// remove everything that's between '<>'
Regex r =  new Regex(@"<.*>");
description = r.Replace(description, "");

对于第二场比赛,结果将是:圣安东尼奥马刺队在中场休息时落后三分,在第一轮季后赛系列赛中与孟菲斯灰熊队以1-1战平87胜利。第一个Feed将保持不变,第三个Feed将自动忽略所有CDATA内容。

答案 1 :(得分:1)