Home > Backend Development > XML/RSS Tutorial > Code example for manually generating comment RSS

Code example for manually generating comment RSS

Y2J
Release: 2017-05-05 09:21:52
Original
1923 people have browsed it

Recently, the discussion atmosphere in the blog park has been lively, and valuable comments have emerged one after another. Sometimes we would like to subscribe to the RSS comments of a certain article, but unfortunately the blog park does not currently have this function. For registered users, we can click the "Subscribe to Reply" link below the comment box to receive emails when new comments appear. It's a pity that anonymous users have to refresh constantly to pay attention to what has been discussed recently. But who are we? We are programmers, and this obstacle should be nothing more than a trivial matter for us. Build your own site, get page data, analyze HTML, and output it as RSS, it's that simple.

Lao Zhao gave the simplest example for this. You can subscribe to the comments of any article on http://jeffreyzhao.cnblogs.com. Because it is just a simple personal tool program, it does not consider performance, scalability, scalability, fault tolerance, and discards any unit testing, dependency injection and other "best practices" . In a word, it comes as easy as it comes.

This example is composed of two groups. The first part is a static HTML page that generates based on the article URL and forwards to its RSS link. Just a few lines of HTML and JavaScript:

<html xmlns="http://www.w3.org/1999/xhtml" >
<head>
    <title>博客园RSS</title>
    <script language="javascript" type="text/javascript">
        function goToCommentRss(url) {
            window.location = "CommentRss.ashx?url=" + encodeURIComponent(url);
        }    </script>
</head>
<body>
    <textarea id="url" cols="50" rows="10"></textarea><br />
    <input type="button" value="Comment RSS"        onclick="goToCommentRss(document.getElementById(&#39;url&#39;).value)" />
</body>
</html>
Copy after login

CommentRss.ashx will output the RSS of the comment for us. Its code is as follows:

public class CommentRss : IHttpHandler{    public void ProcessRequest(HttpContext context)
    {        string url = context.Request.QueryString["url"];        WebClient webClient = new WebClient();
        webClient.Encoding = Encoding.UTF8;        string html = webClient.DownloadString(url);

        context.Response.ContentType = "text/xml";
        context.Response.ContentEncoding = Encoding.UTF8;        SyndicationFeed feed = GetRssFeed(url, html);        Rss20FeedFormatter rssFormatter = new Rss20FeedFormatter(feed);        XmlWriter rssWriter = XmlWriter.Create(context.Response.Output);
        rssFormatter.WriteTo(rssWriter);
        rssWriter.Close();
    }    private static SyndicationFeed GetRssFeed(string url, string html)
    {
        ...
    }    public bool IsReusable { get { return false; } }
}
Copy after login

In fact, the .NET Framework has prepared too many useful tools for us, we just need to splice them together. For example, with the WebClient class, three lines of code can download the HTML of the page. Then we obtain a SyndicationFeed object through the GetRssFeed method, and then output it through Rss20FeedFormatter. SyndicationFeed and Rss20FeedFormatter are both class libraries that come with .NET 3.5 and are placed in the System.ServiceModel.Syndicationnamespace# in the System.ServiceModel.dll assembly. ##, you can easily read or generate XML in Atom 1.0 or RSS 2.0 format for our use. For more information, please refer to this report on the InfoQ Chinese site: WCF's WebProgrammingModelResources.

The key to GetRssReed is to analyze the HTML

String, where Lao Zhao used regular expression to match the title, URL, time, user and content. Then constructing a SyndicationFeed object couldn't be simpler. Unfortunately, the HTML of different templates in the Blog Park is different, so this example of Lao Zhao only supports the current template. You can modify it yourself, for example, add a new parameter to CommentRss.ashx to specify the HTML parsing method, and then it can be used in multiple templates.

This example is also very simple to use. You can compile or deploy it on local IIS, open the Default.html page, and copy the URL of the article, such as "Old Zhao Talks about IL (2)" URL of an article:

Code example for manually generating comment RSS

Click the

button and it will be linked to the RSS page. So it will be displayed in IE as:

Code example for manually generating comment RSS

At this time, you only need to put the URL into the local RSS reader. Because the program is deployed on your machine, you cannot subscribe using tools such as Google Reader. If you have the conditions, just put them in a virtual space or other places. Since your program only serves you, it will not take up a lot of resources, and the current way of writing is enough.

We are programmers. living comfortably without anybody's help.

[Related recommendations]

1.

RSS Efficient Getting Started Tutorial

The above is the detailed content of Code example for manually generating comment RSS. For more information, please follow other related articles on the PHP Chinese website!

Related labels:
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template