更新时间:2022-10-15 18:47:24
以下代码应该做。var firstTwo = docf.Elements( rss) .Elements( channel)。元素( item)。取( 2 )
别忘了使用System.Linq
使用计数器计数到2然后打破循环。
而不是foreach用于循环for ( int i = 0 ;我< 2; i ++)
{
// 做你的东西
}
the code is given below
string rssUri = "https://finance.yahoo.com/news/rss";
var docf = XDocument.Load(rssUri);
foreach (var el in docf.Elements("rss").Elements("channel").Elements("item"))
{
var tit = el.Element("title").Value;
var dec = el.Element("description").Value;
var dat =Convert.ToDateTime( el.Element("pubDate").Value);
}
Thanks in Advance...
The following code shoud do.var firstTwo = docf.Elements("rss").Elements("channel").Elements("item").Take(2)
Don't forget to use"System.Linq"
Use a counter to count till 2 and then break the loop.
Instead of foreach use for loopfor(int i=0; i <2; i++) { //do your stuff }