繁体   English   中英

EntityName解析错误-XPathDocument

[英]EntityName parsing error - XPathDocument

我正在尝试解析来自不同来源的xml提要,一些遵循rss2.0标准的新闻提要,以及其他来自不同来源的信息提要,例如twitter,facebook(实际上为我提供了rss2.0的选项)和linkedIn。 一切都运转良好,直到我将facebook投入使用。 我通过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();

在我的研究中,我发现没有为Facebook feed( 研究 )设置标题。 但是我不能肯定这是否适用,或者是否有一种更好的解决方法。 我曾考虑过SyndicationFeed但由于Twitter不遵循atom或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();
}

我想到了。 由于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