我正在使用以下代码:
private string covertRss(string url)
{
var s = RssReader.Read(url);
StringBuilder sb = new StringBuilder();
foreach (RssNews rs in s) //ERROR LINE
{
sb.AppendLine(rs.Title);
sb.AppendLine(rs.PublicationDate);
sb.AppendLine(rs.Description);
}
return sb.ToString();
}
我收到一个错误:
错误 1 foreach 语句无法对“System.Threading.Tasks.Task(System.Collections.Generic.List(Cricket.MainPage.RssNews))”类型的变量进行操作,因为“System.Threading.Tasks.Task(System.Collections.Generic .List(Cricket.MainPage.RssNews))' 不包含 'GetEnumerator' 的公共定义
RssNews 类是:
public class RssNews
{
public string Title;
public string PublicationDate;
public string Description;
}
我应该添加什么代码才能消除错误并且不影响代码的用途?提前致谢!
RssReader.Read() 的代码
public class RssReader
{
public static async System.Threading.Tasks.Task<List<RssNews>> Read(string url)
{
HttpClient httpClient = new HttpClient();
string result = await httpClient.GetStringAsync(url);
XDocument document = XDocument.Parse(result);
return (from descendant in document.Descendants("item")
select new RssNews()
{
Description = descendant.Element("description").Value,
Title = descendant.Element("title").Value,
PublicationDate = descendant.Element("pubDate").Value
}).ToList();
}
}