我正在尝试解析来自不同来源的xml提要,一些遵循rss2.0标准的新闻提要以及来自不同来源的其他来源,例如twitter,facebook(实际上为我提供rss2.0的选项)和linkedIn。一切都很完美,直到我把facebook扔进了混合。我将rss20作为格式传递,因此它应该遵循与普通rss相同的标准,并且适合我的代码,但它会抛出错误An error occurred while parsing EntityName
并引用此行...
XPathNavigator xpn = new XPathDocument(Server.UrlDecode(XmlHelper.GetXmlFeedUrl(appSettings[x]))).CreateNavigator();
在我的研究中,我发现facebook Feed(research)没有设置标题。但我并不确定这是否适用,或者是否有更好的方法可以解决这个问题。我考虑过SyndicationFeed
,但由于twitter不遵循原子或rss,我认为它不会起作用。
这是facebook网址...
http://www.facebook.com/feeds/page.php?format=rss20%26id=6198772858
这是我的代码......
protected void Page_Load(object sender, EventArgs e)
{
XmlFeedItemPath xfip;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
List<string> xmlFeeds = appSettings.AllKeys.Where(x => x.StartsWith("XmlFeed")).ToList();
string currentXmlFeedType;
if(!string.IsNullOrEmpty(xmlFeedType))
xmlFeeds.RemoveAll(s => !appSettings[s].Contains(XmlFeedType));
xmlFeeds.ForEach(x =>
{
currentXmlFeedType = XmlHelper.GetXmlFeedType(appSettings[x]);
xfip = XmlHelper.GetXmlFeedItemPath(currentXmlFeedType);
XPathNavigator xpn = new XPathDocument(Server.UrlDecode(XmlHelper.GetXmlFeedUrl(appSettings[x]))).CreateNavigator();
XmlNamespaceManager xmlnsm = XmlHelper.GetXmlNameSpaceManager(xpn);
XPathNodeIterator nodes = xpn.Select(xfip.IteratorPath, xmlnsm);
int i = 0;
foreach (XPathNavigator node in nodes)
{
XmlFeedItems.Add(new XmlFeedItem()
{
Title = string.IsNullOrEmpty(xfip.TitlePath) ? xfip.DefaultTitle : node.SelectSingleNode(xfip.TitlePath, xmlnsm).ToString(),
Link = string.IsNullOrEmpty(xfip.LinkPath) ? null : node.SelectSingleNode(xfip.LinkPath, xmlnsm).ToString(),
Teaser = string.IsNullOrEmpty(xfip.TeaserPath) ? null : XmlHelper.WrapUrlWithAnchorTags(node.SelectSingleNode(xfip.TeaserPath, xmlnsm).ToString()),
Source = string.IsNullOrEmpty(xfip.SourcePath) ? null : xpn.SelectSingleNode(xfip.SourcePath, xmlnsm).ToString(),
SortOrder = i,
XmlFeedType = currentXmlFeedType
});
i++;
}
});
rptRssFeed.DataSource = XmlFeedItems.OrderBy(x => x.SortOrder).Take(10);
rptRssFeed.DataBind();
}
答案 0 :(得分:0)
我明白了。由于facebook feed没有useragent字符串,我不得不改变我的实现以使用HttpWebRequest
并为每个xml feed(包括facebook)手动设置一个useragent ....
protected void Page_Load(object sender, EventArgs e)
{
XmlFeedItemPath xfip;
NameValueCollection appSettings = ConfigurationManager.AppSettings;
List<string> xmlFeeds = appSettings.AllKeys.Where(x => x.StartsWith("XmlFeed")).ToList();
string currentXmlFeedType;
if(!string.IsNullOrEmpty(xmlFeedType))
xmlFeeds.RemoveAll(s => !appSettings[s].Contains(XmlFeedType));
xmlFeeds.ForEach(x =>
{
currentXmlFeedType = XmlHelper.GetXmlFeedType(appSettings[x]);
xfip = XmlHelper.GetXmlFeedItemPath(currentXmlFeedType);
var request = (HttpWebRequest)WebRequest.Create(Server.UrlDecode(XmlHelper.GetXmlFeedUrl(appSettings[x])));
request.Method = "GET";
request.UserAgent = "Mozilla/4.0 (compatible; MSIE 8.0; Windows NT 6.0)";
XPathNavigator xpn = new XPathDocument(XmlReader.Create(request.GetResponse().GetResponseStream())).CreateNavigator();
XmlNamespaceManager xmlnsm = XmlHelper.GetXmlNameSpaceManager(xpn);
XPathNodeIterator nodes = xpn.Select(xfip.IteratorPath, xmlnsm);
int i = 0;
foreach (XPathNavigator node in nodes)
{
string publishDate = string.IsNullOrEmpty(xfip.PublishDatePath) ? null : node.SelectSingleNode(xfip.PublishDatePath, xmlnsm).ToString();
XmlFeedItems.Add(new XmlFeedItem()
{
Title = string.IsNullOrEmpty(xfip.TitlePath) ? xfip.DefaultTitle : node.SelectSingleNode(xfip.TitlePath, xmlnsm).ToString(),
Link = string.IsNullOrEmpty(xfip.LinkPath) ? null : node.SelectSingleNode(xfip.LinkPath, xmlnsm).ToString(),
Teaser = string.IsNullOrEmpty(xfip.TeaserPath) ? null : XmlHelper.WrapUrlWithAnchorTags(HttpUtility.HtmlDecode(node.SelectSingleNode(xfip.TeaserPath, xmlnsm).ToString())),
Source = string.IsNullOrEmpty(xfip.SourcePath) ? null : xpn.SelectSingleNode(xfip.SourcePath, xmlnsm).ToString(),
SortOrder = i,
XmlFeedType = currentXmlFeedType,
PublishDate = string.IsNullOrEmpty(publishDate) ? new DateTime() : DateTime.Parse(publishDate.Remove(publishDate.IndexOf(" +")))
});
i++;
}
});
rptRssFeed.DataSource = XmlFeedItems.OrderBy(x => x.GetType().GetProperty(sortField)).Take(10);
rptRssFeed.DataBind();
}