当为RSS提要阅读器编写一些代码时,我得到一个错误"名称'xDocument'不存在于当前上下文中"。xDocument这里有什么问题?
下面是我的代码:
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml.Linq;
namespace RSSreaderAPP
{
public partial class _Default : Page
{
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
ReadRss();
}
}
private void ReadRss()
{
string RssFeedUrl = "http://feeds.feedburner.com/neowin-main";
List<Feeds> feeds = new List<Feeds>();
try
{
XDocument xDoc = new XDocument();
xDoc = xDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("publish date").Value,
description = x.Element("description").Value
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description
};
feeds.Add(f);
}
}
gridViewRSS.DataSource = feeds;
gridViewRSS.DataBind();
}
catch (Exception ex)
{
throw;
}
}
}
}
我改变了
XDocument xDoc = new XDocument();
xDoc = xDocument.Load(RssFeedUrl);
XDocument xDoc = XDocument.Load(RssFeedUrl);
和pubDate = x.Element("publish date").Value
pubDate = x.Element("pubDate").Value,
private void ReadRss()
{
string RssFeedUrl = "http://feeds.feedburner.com/neowin-main";
List<Feeds> feeds = new List<Feeds>();
try
{
//XDocument xDoc = new XDocument();
//xDoc = xDocument.Load(RssFeedUrl);
XDocument xDoc = XDocument.Load(RssFeedUrl);
var items = (from x in xDoc.Descendants("item")
select new
{
title = x.Element("title").Value,
link = x.Element("link").Value,
pubDate = x.Element("pubDate").Value,
description = x.Element("description").Value,
});
if (items != null)
{
foreach (var i in items)
{
Feeds f = new Feeds
{
Title = i.title,
Link = i.link,
PublishDate = i.pubDate,
Description = i.description,
};
feeds.Add(f);
}
}
gridViewRSS.DataSource = feeds;
gridViewRSS.DataBind();
}
catch (Exception ex)
{
throw;
}
}