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

前端之家收集整理的这篇文章主要介绍了如何在Windows 8应用程序中使用c#添加facebook rss 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
}

祝你好运!

猜你在找的Windows相关文章