简体   繁体   English

EntityName解析错误-XPathDocument

[英]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. 我正在尝试解析来自不同来源的xml提要,一些遵循rss2.0标准的新闻提要,以及其他来自不同来源的信息提要,例如twitter,facebook(实际上为我提供了rss2.0的选项)和linkedIn。 Everything was working perfectly until I threw facebook into the mix. 一切都运转良好,直到我将facebook投入使用。 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... 我通过rss20作为格式,因此它应遵循与普通rss提要相同的标准,并适合我的代码,但是会An error occurred while parsing EntityName并引用此行时An error occurred while parsing EntityName

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 ). 在我的研究中,我发现没有为Facebook feed( 研究 )设置标题。 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. 我曾考虑过SyndicationFeed但由于Twitter不遵循atom或rss,所以我认为它不会起作用。

Here's the facebook url... 这是facebook网址...

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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