我收到错误"名称' xDocument'在当前背景下不存在"为RSS feed阅读器编写一些代码时。 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;
}
}
}
}
这是为了避免烦人的消息。
答案 0 :(得分:0)
我改变了
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;
}
}