如何在Windows 8应用程序中使用c#添加facebook rss feed?
发布时间:2020-12-14 05:25:22 所属栏目:Windows 来源:网络整理
导读:我目前正在使用C#和XAML开发一个 Windows 8 metro应用程序.我正在使用Syndication方法从多个来源阅读RSS源,并且它工作正常,但FACEBOOK的RSS实际上并没有崩溃,并且错误“INVALID XML”.读它作为XmlDocument工作吗?怎么做? 以下是我的代码: using System;us
我目前正在使用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 } 祝你好运! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |
相关内容
- microsoft-graph – 在Microsoft Graph / me / drive / sea
- windows – Powershell Get-Process负内存值
- Windows批处理脚本来取消隐藏病毒隐藏的文件
- 在Windows OpenGL中显示诊断文本的最佳方法
- Win7下mysql的安装
- 在Win32 Delphi应用程序中存储用户首选项和设置的最佳实践是
- 在win7中通过手机投放媒体
- 添加存储在windows批处理脚本中的变量中的数字
- REPL挂在SublimeREPL for Clojure(Windows PC)
- 有没有用libgit2,libgit2sharp,Ngit用msxgit替换windows?
推荐文章
站长推荐
热点阅读