gpt4 book ai didi

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

转载 作者:行者123 更新时间:2023-11-30 12:11:58 25 4
gpt4 key购买 nike

我目前正在使用 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/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com