Max*_*sky 2 .net c# rss serialization rss.net
我正在编写Stack Overflow API包装器,目前位于http://soapidotnet.googlecode.com/.我有一些关于解析SO RSS提要的问题.
我已经选择使用RSS.NET来解析RSS,但是我对我的代码有一些疑问(我在本文中进一步介绍过).
首先,我正确解析这些属性吗?我有一个名为Question的类,它有这些属性.
接下来,我如何解析<re:rank>RSS属性(用于投票数)?我不确定RSS.NET如何让我们这样做.据我所知,它是一个带有自定义命名空间的元素.
最后,我是否必须手动添加所有属性,就像我目前在我的代码中一样?他们可以使用某种反序列化吗?
以下是我目前解析最近问题提要的代码:
/// <summary>
/// Utilises recent question feeds to obtain recently updated questions on a certain site.
/// </summary>
/// <param name="site">Trilogy site in question.</param>
/// <returns>A list of objects of type Question, which represents the recent questions on a trilogy site.</returns>
public static List<Question> GetRecentQuestions(TrilogySite site)
{
List<Question> RecentQuestions = new List<Question>();
RssFeed feed = RssFeed.Load(string.Format("http://{0}.com/feeds",GetSiteUrl(site)));
RssChannel channel = (RssChannel)feed.Channels[0];
foreach (RssItem item in channel.Items)
{
Question toadd = new Question();
foreach(RssCategory cat in item.Categories)
{
toadd.Categories.Add(cat.Name);
}
toadd.Author = item.Author;
toadd.CreatedDate = ConvertToUnixTimestamp(item.PubDate).ToString();
toadd.Id = item.Link.Url.ToString();
toadd.Link = item.Link.Url.ToString();
toadd.Summary = item.Description;
//TODO: OTHER PROPERTIES
RecentQuestions.Add(toadd);
}
return RecentQuestions;
}
Run Code Online (Sandbox Code Playgroud)
以下是该SO RSS源的代码:
<feed xmlns="http://www.w3.org/2005/Atom" xmlns:creativeCommons="http://backend.userland.com/creativeCommonsRssModule" xmlns:re="http://purl.org/atompub/rank/1.0">
<title type="text">Top Questions - Stack Overflow</title>
<link rel="self" href="http://stackoverflow.com/feeds" type="application/atom+xml" />
<link rel="alternate" href="http://stackoverflow.com/questions" type="text/html" />
<subtitle>most recent 30 from stackoverflow.com</subtitle>
<updated>2009-11-28T19:26:49Z</updated>
<id>http://stackoverflow.com/feeds</id>
<creativeCommons:license>http://www.creativecommons.org/licenses/by-nc/2.5/rdf</creativeCommons:license>
<entry>
<id>http://stackoverflow.com/questions/1813483/averaging-angles-again</id>
<re:rank scheme="http://stackoverflow.com">0</re:rank>
<title type="text">Averaging angles... Again</title>
<category scheme="http://stackoverflow.com/feeds/tags" term="algorithm"/><category scheme="http://stackoverflow.com/feeds/tags" term="math"/><category scheme="http://stackoverflow.com/feeds/tags" term="geometry"/><category scheme="http://stackoverflow.com/feeds/tags" term="calculation"/>
<author><name>Lior Kogan</name></author>
<link rel="alternate" href="http://stackoverflow.com/questions/1813483/averaging-angles-again" />
<published>2009-11-28T19:19:13Z</published>
<updated>2009-11-28T19:26:39Z</updated>
<summary type="html">
<p>I want to calculate the average of a set of angles.</p>
<p>I know it has been discussed before (several times). The accepted answer was <strong>Compute unit vectors from the angles and take the angle of their average</strong>.</p>
<p>However this answer defines the average in a non intuitive way. The average of 0, 0 and 90 will be <strong>atan( (sin(0)+sin(0)+sin(90)) / (cos(0)+cos(0)+cos(90)) ) = atan(1/2)= 26.56 deg</strong> </p>
<p>I would expect the average of 0, 0 and 90 to be 30 degrees.</p>
<p>So I think it is fair to ask the question again: How would you calculate the average, so such examples will give the intuitive expected answer.</p>
</summary>
</entry>
Run Code Online (Sandbox Code Playgroud)
等等
这是我的问题类,如果有帮助:
/// <summary>
/// Represents a question.
/// </summary>
public class Question : Post //TODO: Have Question and Answer derive from Post
{
/// <summary>
/// # of favorites.
/// </summary>
public double FavCount { get; set; }
/// <summary>
/// # of answers.
/// </summary>
public double AnswerCount { get; set; }
/// <summary>
/// Tags.
/// </summary>
public string Tags { get; set; }
}
/// <summary>
/// Represents a post on Stack Overflow (question, answer, or comment).
/// </summary>
public class Post
{
/// <summary>
/// Id (link)
/// </summary>
public string Id { get; set; }
/// <summary>
/// Number of votes.
/// </summary>
public double VoteCount { get; set; }
/// <summary>
/// Number of views.
/// </summary>
public double ViewCount { get; set; }
/// <summary>
/// Title.
/// </summary>
public string Title { get; set; }
/// <summary>
/// Created date of the post (expressed as a Unix timestamp)
/// </summary>
public string CreatedDate
{
get
{
return CreatedDate;
}
set
{
CreatedDate = value;
dtCreatedDate = StackOverflow.ConvertFromUnixTimestamp(StackOverflow.ExtractTimestampFromJsonTime(value));
}
}
/// <summary>
/// Created date of the post (expressed as a DateTime)
/// </summary>
public DateTime dtCreatedDate { get; set; }
/// <summary>
/// Last edit date of the post (expressed as a Unix timestamp)
/// </summary>
public string LastEditDate
{
get
{
return LastEditDate;
}
set
{
LastEditDate = value;
dtLastEditDate = StackOverflow.ConvertFromUnixTimestamp(StackOverflow.ExtractTimestampFromJsonTime(value));
}
}
/// <summary>
/// Last edit date of the post (expressed as a DateTime)
/// </summary>
public DateTime dtLastEditDate { get; set; }
/// <summary>
/// Author of the post.
/// </summary>
public string Author { get; set; }
/// <summary>
/// HTML of the post.
/// </summary>
public string Summary { get; set; }
/// <summary>
/// URL of the post.
/// </summary>
public string Link { get; set; }
/// <summary>
/// RSS Categories (or tags) of the post.
/// </summary>
public List<string> Categories { get; set; }
}
Run Code Online (Sandbox Code Playgroud)
提前致谢! 顺便说一句,请为图书馆项目做贡献!:)
Mar*_*eck 10
首先,我从未使用过RSS.NET,但我想知道你是否意识到.NET框架在System.ServiceModel.Syncidation命名空间中有自己的RSS api .这个SyndicationFeed课程就是这个课程的起点.
为了解决你的问题,我写了一个小样本,它接受了这个问题的提要,并将标题,作者,id和等级(你感兴趣的扩展元素)写出到控制台.这应该有助于向您展示此API的简单程度以及如何访问排名.
// load the raw feed
using (var xmlr = XmlReader.Create("https://stackoverflow.com/feeds/question/1813559"))
{
// get the items within a feed
var feedItems = SyndicationFeed
.Load(xmlr)
.GetRss20Formatter()
.Feed
.Items;
// print out details about each item in the feed
foreach (var item in feedItems)
{
Console.WriteLine("Title: {0}", item.Title.Text);
Console.WriteLine("Author: {0}", item.Authors.First().Name);
Console.WriteLine("Id: {0}", item.Id);
// the extensions assume that there can be more than one value, so get
// the first or default value (default == 0)
int rank = item.ElementExtensions
.ReadElementExtensions<int>("rank", "http://purl.org/atompub/rank/1.0")
.FirstOrDefault();
Console.WriteLine("Rank: {0}", rank);
}
}
Run Code Online (Sandbox Code Playgroud)
以上代码导致以下内容写入控制台...
标题:.NET/C#:将RSS.NET与Stack Overflow Feeds一起使用:如何处理RSS项的特殊属性
作者:Maxim Z.
Id:.NET/C#:使用RSS.NET和Stack Overflow Feeds:如何处理RSS项的特殊属性?
排名:0
有关SyndicationFeed类的更多信息,请访问此处...
http://msdn.microsoft.com/en-us/library/system.servicemodel.syndication.syndicationfeed.aspx
有关从RSS源读取和写入扩展值的一些示例,请转到此处...
http://msdn.microsoft.com/en-us/library/bb943475.aspx
关于创建你的问题实例,我不确定序列化的快速获胜.我可能会写一些更像这样的代码......
var questions = from item in feedItems
select
new Question
{
Title = item.Title.Text,
Author = item.Authors.First().Name,
Id = item.Id,
Rank = item.ElementExtensions.ReadElementExtensions<int>(
"rank", "http://purl.org/atompub/rank/1.0").FirstOrDefault()
};
Run Code Online (Sandbox Code Playgroud)
......但它几乎做同样的事情.
上面的内容需要安装.NET 3.5库.以下没有,但需要C#3.5(将创建以.NET 2.0为目标的程序集)
我建议你考虑一件事 - 不要创建自定义类型,而是编写SyndicationItem类型的扩展方法.如果您让用户处理SyndicationType(支持,理解,记录等类型),但添加扩展方法以便更轻松地访问SO特定属性,那么您可以使用户的生活更轻松,并且当您的用户生活时,他们总是可以回退到SyndicationItem API.这些扩展并没有达到他们想要的效果.所以,例如,如果你写了这个扩展方法......
public static class SOExtensions
{
public static int Rank(this SyndicationItem item)
{
return item.ElementExtensions
.ReadElementExtensions<int>("rank", "http://purl.org/atompub/rank/1.0")
.FirstOrDefault();
}
}
Run Code Online (Sandbox Code Playgroud)
...你可以像这样访问SyndicationItem的排名......
Console.WriteLine("Rank: {0}", item.Rank());
Run Code Online (Sandbox Code Playgroud)
...当SO添加一些其他扩展属性到您没有为API用户提供的Feed时,可以回退到查看ElementExtensions集合.
最后一次更新......
我没有使用过Rss.NET库,但我已经阅读了在线文档.从最初阅读这些文档开始,我建议没有办法访问您尝试访问的扩展元素(项目的等级).如果RSS.NET API允许访问给定RssItem的xml(并且我不确定它是什么),那么您可以使用扩展方法机制来扩充RssItem类.
我发现SyndicationFeed API非常强大且非常容易掌握,所以如果使用.NET 3.5是一个选项,那么我就会朝那个方向前进.