User Tools

Site Tools


notes:asp.net:rss_opml

RSS/OPML

An example of generating a feeds.opml file that can be subscribed to using an RSS client such as Internet Explorer (Import and export RSS feeds in Internet Explorer). The following code creates a button that generates the feeds.opml file:

<asp:Button ID="btnSubscribe" Text="Subscribe" OnClick="btnSubscribe_Click" runat="server" />
using System;
using System.Collections.Generic;
using System.IO;
using System.Text;
using System.Xml;
 
namespace WebTest
{
    public partial class Default : System.Web.UI.Page
    {
        private const string OpmlFilename = "feeds.opml";
 
        protected void Page_Load(object sender, EventArgs e)
        {
        }
 
        protected void btnSubscribe_Click(object sender, EventArgs e)
        {
            List<RssOutline> outlines = new List<RssOutline>();
 
            // Populate the outlines.
            outlines.Add(new RssOutline { Title = "Title1", Url = "http://website.com/page1" });
            outlines.Add(new RssOutline { Title = "Title2", Url = "http://website.com/page2" });
            outlines.Add(new RssOutline { Title = "Title3", Url = "http://website.com/page3" });
 
            if (outlines.Count > 0)
            {
                byte[] contents = GenerateOPML(outlines, "Leon Smith");
                ReturnOPML(contents);
            }
        }
 
        private byte[] GenerateOPML(List<RssOutline> outlines, string ownerName)
        {
            Byte[] bytes;
 
            string dateCreated = DateTime.Now.ToString("R");
 
            using (MemoryStream stream = new MemoryStream())
            {
                using (XmlTextWriter w = new XmlTextWriter(stream, Encoding.UTF8))
                {
                    // Format XML with indentation.
                    w.Formatting = Formatting.Indented;
 
                    // Write the XML declaration.
                    w.WriteStartDocument();
 
                    // Write the root element.
                    w.WriteStartElement("opml");
                    w.WriteAttributeString("version", "1.0");
 
                    // Write the header.
                    w.WriteStartElement("head");
                    w.WriteStartElement("title"); w.WriteString("RSS Feeds"); w.WriteEndElement();
                    w.WriteStartElement("dateCreated"); w.WriteString(dateCreated); w.WriteEndElement();
                    w.WriteStartElement("dateModified"); w.WriteString(dateCreated); w.WriteEndElement();
                    w.WriteStartElement("ownerName"); w.WriteString(ownerName); w.WriteEndElement();
                    w.WriteEndElement();
 
                    // Write the body.
                    w.WriteStartElement("body");
 
                    // Write each record to the stream.
                    foreach (RssOutline line in outlines)
                    {
                        w.WriteStartElement("outline");
 
                        // Write attributes. 
                        w.WriteAttributeString("title", line.Title);
                        w.WriteAttributeString("text", line.Title);
                        w.WriteAttributeString("xmlUrl", line.Url);
                        w.WriteAttributeString("type", "rss");
                        w.WriteAttributeString("dateCreated", dateCreated);
 
                        w.WriteEndElement(); // close the outline element
                    }
 
                    // Close the body element.
                    w.WriteEndElement();
 
                    // Close the root element.
                    w.WriteEndElement();
 
                    // Close the XML document.
                    w.WriteEndDocument();
                }
 
                // Convert the MemoryStream to an array.
                bytes = stream.ToArray();
            }
 
            return bytes;
        }
 
        private void ReturnOPML(byte[] contents)
        {
            this.Response.Clear();
            this.Response.AddHeader("Content-Disposition", "attachment; filename=" + OpmlFilename);
            this.Response.ContentType = "application/xml";
            this.Response.BinaryWrite(contents);
            this.Response.End();
        }
    }
 
    // A class describing a single RSS outline.
    public class RssOutline
    {
        public string Title { get; set; }
        public string Url { get; set; }
    }
}
notes/asp.net/rss_opml.txt · Last modified: 2016/04/19 by admin