简体   繁体   中英

EntityName parsing error - XPathDocument

I am trying to parse out xml feeds from different sources, some news feeds following the rss2.0 standard and others from different sources like twitter, facebook (which actually gives me the option for rss2.0) and linkedIn. Everything was working perfectly until I threw facebook into the mix. I passed rss20 as the format so it should follow the same standards as a normal rss feed and fit right into my code but it's throwing the error An error occurred while parsing EntityName and referencing this line...

XPathNavigator xpn = new XPathDocument(Server.UrlDecode(XmlHelper.GetXmlFeedUrl(appSettings[x]))).CreateNavigator();

In my research I found that there is no header set for the facebook feed ( research ). But I'm not quit sure this applies or if there is a completely better way to go about this. I've thought about SyndicationFeed but since twitter doesn't follow atom or rss I don't think it would work.

Here's the facebook url...

http://www.facebook.com/feeds/page.php?format=rss20%26id=6198772858

Here is my code...

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();
}

I figured it out. Since the facebook feed did not have a useragent string I had to change my implementation to use HttpWebRequest and manually set a useragent for each xml feed (facebook included)....

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();
} 

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM