search
HomeBackend DevelopmentXML/RSS TutorialCode example for manually generating comment RSS

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>

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

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!

Statement
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
Mastering Well-Formed XML: Best Practices for Data ExchangeMastering Well-Formed XML: Best Practices for Data ExchangeMay 14, 2025 am 12:05 AM

Well-formedXMLiscrucialfordataexchangebecauseitensurescorrectparsingandunderstandingacrosssystems.1)Startwithadeclarationlike.2)Ensureeveryopeningtaghasaclosingtagandelementsareproperlynested.3)Useattributescorrectly,enclosingvaluesinquotesandavoidin

XML: Is it still used?XML: Is it still used?May 13, 2025 pm 03:13 PM

XMLisstillusedduetoitsstructurednature,humanreadability,andwidespreadadoptioninenterpriseenvironments.1)Itfacilitatesdataexchangeinsectorslikefinance(SWIFT)andhealthcare(HL7).2)Itshuman-readableformataidsinmanualdatainspectionandediting.3)XMLisusedin

The Anatomy of an RSS Document: Structure and ElementsThe Anatomy of an RSS Document: Structure and ElementsMay 10, 2025 am 12:23 AM

The structure of an RSS document includes three main elements: 1.: root element, defining the RSS version; 2.: Containing channel information, such as title, link, and description; 3.: Representing specific content entries, including title, link, description, etc.

Understanding RSS Documents: A Comprehensive GuideUnderstanding RSS Documents: A Comprehensive GuideMay 09, 2025 am 12:15 AM

RSS documents are a simple subscription mechanism to publish content updates through XML files. 1. The RSS document structure consists of and elements and contains multiple elements. 2. Use RSS readers to subscribe to the channel and extract information by parsing XML. 3. Advanced usage includes filtering and sorting using the feedparser library. 4. Common errors include XML parsing and encoding issues. XML format and encoding need to be verified during debugging. 5. Performance optimization suggestions include cache RSS documents and asynchronous parsing.

RSS, XML and the Modern Web: A Content Syndication Deep DiveRSS, XML and the Modern Web: A Content Syndication Deep DiveMay 08, 2025 am 12:14 AM

RSS and XML are still important in the modern web. 1.RSS is used to publish and distribute content, and users can subscribe and get updates through the RSS reader. 2. XML is a markup language and supports data storage and exchange, and RSS files are based on XML.

Beyond Basics: Advanced RSS Features Enabled by XMLBeyond Basics: Advanced RSS Features Enabled by XMLMay 07, 2025 am 12:12 AM

RSS enables multimedia content embedding, conditional subscription, and performance and security optimization. 1) Embed multimedia content such as audio and video through tags. 2) Use XML namespace to implement conditional subscriptions, allowing subscribers to filter content based on specific conditions. 3) Optimize the performance and security of RSSFeed through CDATA section and XMLSchema to ensure stability and compliance with standards.

Decoding RSS: An XML Primer for Web DevelopersDecoding RSS: An XML Primer for Web DevelopersMay 06, 2025 am 12:05 AM

RSS is an XML-based format used to publish frequently updated data. As a web developer, understanding RSS can improve content aggregation and automation update capabilities. By learning RSS structure, parsing and generation methods, you will be able to handle RSSfeeds confidently and optimize your web development skills.

JSON vs. XML: Why RSS Chose XMLJSON vs. XML: Why RSS Chose XMLMay 05, 2025 am 12:01 AM

RSS chose XML instead of JSON because: 1) XML's structure and verification capabilities are better than JSON, which is suitable for the needs of RSS complex data structures; 2) XML was supported extensively at that time; 3) Early versions of RSS were based on XML and have become a standard.

See all articles

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

SublimeText3 Linux new version

SublimeText3 Linux new version

SublimeText3 Linux latest version

MantisBT

MantisBT

Mantis is an easy-to-deploy web-based defect tracking tool designed to aid in product defect tracking. It requires PHP, MySQL and a web server. Check out our demo and hosting services.

Zend Studio 13.0.1

Zend Studio 13.0.1

Powerful PHP integrated development environment

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

VSCode Windows 64-bit Download

VSCode Windows 64-bit Download

A free and powerful IDE editor launched by Microsoft