使用visualc阅读RSS提要
我正在尝试读取RSS提要并在我的C应用程序中显示。我使用了下面的代码,它可以完美地用于其他RSS源。我想阅读此RSS提要-->但下面的代码不适用于它。我没有收到任何错误,但什么也没有发生,我希望RSS提要显示的文本框是空的。请帮忙。我做错了什么使用visualc阅读RSS提要,rss,visual-c#-express-2010,rss-reader,Rss,Visual C# Express 2010,Rss Reader,我正在尝试读取RSS提要并在我的C应用程序中显示。我使用了下面的代码,它可以完美地用于其他RSS源。我想阅读此RSS提要-->但下面的代码不适用于它。我没有收到任何错误,但什么也没有发生,我希望RSS提要显示的文本框是空的。请帮忙。我做错了什么 public class RssNews { public string Title; public string PublicationDate; public string Descri
public class RssNews
{
public string Title;
public string PublicationDate;
public string Description;
}
public class RssReader
{
public static List<RssNews> Read(string url)
{
var webResponse = WebRequest.Create(url).GetResponse();
if (webResponse == null)
return null;
var ds = new DataSet();
ds.ReadXml(webResponse.GetResponseStream());
var news = (from row in ds.Tables["item"].AsEnumerable()
select new RssNews
{
Title = row.Field<string>("title"),
PublicationDate = row.Field<string>("pubDate"),
Description = row.Field<string>("description")
}).ToList();
return news;
}
}
private string covertRss(string url)
{
var s = RssReader.Read(url);
StringBuilder sb = new StringBuilder();
foreach (RssNews rs in s)
{
sb.AppendLine(rs.Title);
sb.AppendLine(rs.PublicationDate);
sb.AppendLine(rs.Description);
}
return sb.ToString();
}
似乎DataSet.ReadXml方法失败了,因为在项中指定了两次category,但是在不同的命名空间下 这似乎效果更好:
public static List<RssNews> Read(string url)
{
var webClient = new WebClient();
string result = webClient.DownloadString(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();
}
非常感谢你。工作完美:
public static List<RssNews> Read(string url)
{
var webClient = new WebClient();
string result = webClient.DownloadString(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();
}