c# - 如何在 Windows 8 应用程序中使用 c# 添加 facebook rss 提要?

标签 c# windows-8 rss windows-store-apps syndication-feed

我目前正在使用 C# 和 XAML 开发 Windows 8 Metro 应用程序。我正在使用 Syndication 方法从多个来源读取 RSS 提要,它工作得很好,但来自 FACEBOOK 的 RSS 实际上不是,并且正在崩溃并出现错误“INVALID XML”。将它作为 XmlDocument 读取是否有效?怎么做?

下面是我的代码:

using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Net;
using System.Net.Http.Headers;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Linq;
using Windows.Data.Xml.Dom;
using Windows.Web.Syndication;



namespace App1
{

public class FeedData
{
    public string title { get; set; }
    public string description { get; set; }
    public DateTime pubDate { get; set; }

    private List<FeedItem> _Items = new List<FeedItem>();
    public List<FeedItem> Items
    {
        get
        {
            return this._Items;
        }
    }
}

// FeedItem 
// Holds info for a single blog post 
public class FeedItem
{
    public string title { get; set; }
    public string description { get; set; }
    public DateTime pubDate { get; set; }
    public Uri link { get; set; }
}

// FeedDataSource 
// Holds a collection of blog feeds (FeedData), and contains methods needed to 
// retreive the feeds. 
public class FeedDataSource
{
    private ObservableCollection<FeedData> _Feeds = new ObservableCollection<FeedData>();
    public ObservableCollection<FeedData> Feeds
    {
        get
        {
            return this._Feeds;
        }
    }



    public async Task GetFeedsAsync(Int32 ID)
    {
        if (ID == 1003)
        {
            Task<FeedData> feed1 =
            GetFeedAsync("http://outbound.indevcogroup.com/feeds/posts/default/-/INDEVCO%20Group");
            this.Feeds.Add(await feed1);
        }
        else if (ID == 1002)
        {
            Task<FeedData> feed12 =
          GetFeedAsync("http://www.facebook.com/feeds/page.php?format=rss20&id=126332847400326");
            this.Feeds.Add(await feed12);
        }
    }


    private async Task<FeedData> GetFeedAsync(string feedUriString)
    {
        // using Windows.Web.Syndication; 
        SyndicationClient client = new SyndicationClient();
        Uri feedUri = new Uri(feedUriString);

        try
        {

            SyndicationFeed feed = await client.RetrieveFeedAsync(feedUri);

            // This code is executed after RetrieveFeedAsync returns the SyndicationFeed. 
            // Process it and copy the data we want into our FeedData and FeedItem classes. 
            FeedData feedData = new FeedData();

            feedData.title = feed.Title.Text;
            if (feed.Subtitle != null && feed.Subtitle.Text != null)
            {
                feedData.description = feed.Subtitle.Text;
            }
            // Use the date of the latest post as the last updated date. 
            feedData.pubDate = feed.Items[0].PublishedDate.DateTime;

            foreach (SyndicationItem item in feed.Items)
            {
                FeedItem feedItem = new FeedItem();
                feedItem.title = item.Title.Text;
                feedItem.pubDate = item.PublishedDate.DateTime;
                // Handle the differences between RSS and Atom feeds. 
                if (feed.SourceFormat == SyndicationFormat.Atom10)
                {
                    feedItem.description = item.Content.Text;
                    feedItem.link = new Uri("http://www.scoop.it/t/agricultural-horticultural-                        news" + item.Source);
                }
                else if (feed.SourceFormat == SyndicationFormat.Rss20)
                {
                    feedItem.description = item.Summary.Text;
                    feedItem.link = item.Links[0].Uri;
                }
                feedData.Items.Add(feedItem);
            }
            return feedData;
        }
        catch (Exception)
        {
            return null;
        }
    }
}

最佳答案

像这样:

// this example pulls coca-cola's posts
var _Uri = new Uri("http://www.facebook.com/feeds/page.php?format=rss20&id=40796308305");

// including user agent, otherwise FB rejects the request
var _Client = new HttpClient();
_Client.DefaultRequestHeaders.Add("user-agent", "Mozilla/5.0 (compatible; MSIE 10.0; Windows NT 6.2; WOW64; Trident/6.0)");

// fetch as string to avoid error
var _Response = await _Client.GetAsync(_Uri);
var _String = await _Response.Content.ReadAsStringAsync();

// convert to xml (will validate, too)
var _XmlDocument = new Windows.Data.Xml.Dom.XmlDocument();
_XmlDocument.LoadXml(_String);

// manually fill feed from xml
var _Feed = new Windows.Web.Syndication.SyndicationFeed();
_Feed.LoadFromXml(_XmlDocument);

// continue as usual...
foreach (var item in _Feed.Items)
{
    // do something
}

祝你好运!

关于c# - 如何在 Windows 8 应用程序中使用 c# 添加 facebook rss 提要?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/14395514/

相关文章:

c# - 在 C# 中使用 "params"关键字作为泛型参数

c# - 如何将全局异常处理程序添加到 Metro Style 应用程序?

c++ - Windows 8/Metro 认证要求 - 如何检查?

php - 前置此 PHP 文件

javascript - Selenium 不显示文本

c# - 找不到远程主机 "."错误

rss - 如何将 RSS 提要内联转换为 Atom?

java - dom4j 未加载整个 xml

c# - app.config 中的 maxReceivedMessageSize 和 maxBufferSize

c# - 如何在 c# windows 8 Metro App 中从主机名获取 IP 地址?