我正在编写 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;
}
这是该 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>
等等
这是我的问题课,如果有帮助的话:
/// <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; }
}
提前致谢! 顺便说一句,请为图书馆项目做出贡献!:)