2

我正在编写 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"> 
            &lt;p&gt;I want to calculate the average of a set of angles.&lt;/p&gt;

&lt;p&gt;I know it has been discussed before (several times). The accepted answer was &lt;strong&gt;Compute unit vectors from the angles and take the angle of their average&lt;/strong&gt;.&lt;/p&gt;

&lt;p&gt;However this answer defines the average in a non intuitive way. The average of 0, 0 and 90 will be &lt;strong&gt;atan( (sin(0)+sin(0)+sin(90)) / (cos(0)+cos(0)+cos(90)) ) = atan(1/2)= 26.56 deg&lt;/strong&gt; &lt;/p&gt;

&lt;p&gt;I would expect the average of 0, 0 and 90 to be 30 degrees.&lt;/p&gt;

&lt;p&gt;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.&lt;/p&gt;

        </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; }

    }

提前致谢! 顺便说一句,请为图书馆项目做出贡献!:)

4

1 回答 1

10

首先,我从未使用过 RSS.NET,但我想知道您是否意识到 .NET 框架在System.ServiceModel.Syncidation名称空间中有自己的 RSS api。课程是这个SyndicationFeed的起点。

为了解决您的问题,我编写了一个小示例,该示例获取该问题的提要,并将titleauthoridrank(您感兴趣的扩展元素)写入控制台。这应该有助于向您展示此 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);  
    }
}

上面的代码导致以下内容被写入控制台......

标题:.NET/C#:将 RSS.NET 与堆栈溢出提要一起使用:如何处理 RSS 项的特殊属性

作者:马克西姆 Z。

ID:.NET/C#:将 RSS.NET 与堆栈溢出提要一起使用:如何处理 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

关于创建您的 Question 实例,我不确定序列化是否能迅速取胜。我可能会写你的代码更像这样......

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()
                        };

...但它几乎在做同样的事情。

上面的东西需要安装 .NET 3.5 库。以下不是,但需要 C# 3.5(它将创建面向 .NET 2.0 的程序集)

我建议您考虑一件事 - 不要创建自定义类型,而是为 SyndicationItem 类型编写扩展方法。如果您让您的用户处理 SyndicationType(一种受支持、理解、记录等的类型),但添加扩展方法以更轻松地访问 SO 特定属性,那么您可以让用户的生活更轻松,并且当您使用 SyndicationItem API 时,他们总是可以退回到 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();
    }
}

...您可以像这样访问 SyndicationItem 的等级...

Console.WriteLine("Rank: {0}", item.Rank());  

...并且当 SO 添加一些其他扩展属性到您没有满足您的 API 用户的提要时,可以回退到查看 ElementExtensions 集合。

最后一次更新...

我没有使用过 Rss.NET 库,但我已经阅读了在线文档。从对这些文档的初步阅读中,我建议没有办法访问您尝试访问的扩展元素(项目的等级)。如果 RSS.NET API 允许访问给定 RssItem 的 xml(我不确定是否如此),那么您可以使用扩展方法机制来扩充 RssItem 类。

我发现 SyndicationFeed API 非常强大且易于掌握,因此如果您可以选择使用 .NET 3.5,那么我会朝这个方向发展。

于 2009-12-07T21:08:39.560 回答