Month: August 2007

rss 2.0 feed format tutorial for .NET

Posted by – August 22, 2007

I have been creating custom blog software using csharp.net. One task that I accomplished today was getting both an RSS and an ATOM feed setup. This will allow people to read the blog on sites like netvibes, my yahoo, or google IG. So heres how I did it:

  1. Change the content type of the page to text/xml

    This is needed so that other computers and browsers will know that it is an XML page they are reading, and not the standard HTML. Luckily this is simple to do, simply add ContentType=”text/xml” to the Page directive, like so:

    
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="rss.aspx.cs" Inherits="rss" ContentType="text/xml"%>
    
  2. Setup RSS 2.0 XML Document

    Now in our code behind file, we can write directly to the output stream, as the proper header is already set. You may think about using Response.Write to create the RSS XML tags, but there is a much easier way. You can use an XmlTextWriter to achieve the same result, without needing to escape all characters that can’t normally be in an xml file

    
            // Create a new XmlTextWriter instance
            XmlTextWriter writer = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);
    
            // Create Document
            writer.WriteStartDocument();
                writer.WriteStartElement("rss");
                writer.WriteAttributeString("version", "2.0");
                    //Set Channel Information
                    writer.WriteStartElement("channel");
                        writer.WriteElementString("title","[SITE_TITLE]");
                        writer.WriteElementString("link",[SITE_URL]);
                        writer.WriteElementString("description",[SITE DESCRIPTION]);
                        writer.WriteElementString("lastBuildDate",[RFC-822 DATE]);
    

    Here we setup the start of the XML document with all required fields. The lastBuildDate element should be the last time the xml feed has changed, which should also be the last time something new was posted.

  3. Output Post information

    The standard is to display them in reverse chronological order. Most likely you will be creating these in a loop, like so:

    
    for (int i = 0; i < postList.Count; i++)
    {
    
        writer.WriteStartElement("item");
            writer.WriteElementString("title",postList[i].pageTitle);
            writer.WriteElementString("link", postList[i].url);
            writer.WriteElementString("description", postList[i].content);
            writer.WriteElementString("pubDate", postList[i].postDate);
            writer.WriteElementString("guid", postList[i].url);
        writer.WriteEndElement();
    }
    

    Here I am assuming postList is a list of object containing post information. The pubDate tag is the date that the post was published, and guid is a globally unique identifier for the post. If you have a GUID setup in the database for the post you can use it, otherwise it's easiest to just use the permalink (url).

  4. Close Document

    Now that we have displayed all relevant information, we simply need to close open tags, and end the xml document

    
                    writer.WriteEndElement();
                writer.WriteEndElement();
            writer.WriteEndDocument();
    writer.Close();    
    

    Thats all there is to it. You can now use this page to submit to sites such as netvibes, google ig, my yahoo, or any other services that will parse rss 2.0 xml feeds.

CSharp Blog ping code using xml-rpc and weblogupdates.ping for Pingomatic, Technorati

Posted by – August 1, 2007

I am currently writing a custom blog application in csharp.net (c#.net). One requirement for the software was that it needed to send update information to sites like Technorati or Ping-o-matic. After some research, I’ve found that sites allow you to do this utilizing xml-rpc. The process is called pingback, but it more commenly refered to as simply pinging. Basically every time you write a new post, you can ping these services to tell them you have updated content, and let people know about it.

The specific method we want to access via xml-rpc is called “weblogUpdates.ping”. This function is implemented on all blog ping services. Below is a function which defines a list of sites to ping, then goes through and pings them one by one. It takes as parameters the name of your site, and the url of your site.


private void sendPing(String name, String url)
{
ArrayList listToPing = new ArrayList();
listToPing.Add("http://rpc.technorati.com/rpc/ping");
listToPing.Add("http://rpc.pingomatic.com");
listToPing.Add("http://blogsearch.google.com/ping/RPC2");

for (int i = 0; i < listToPing.Count; i++)
{
    HttpWebRequest request = (HttpWebRequest)WebRequest.Create(listToPing[i].ToString());
    request.Method = "POST";
    request.ContentType = "text/xml";

    Stream stream = (Stream)request.GetRequestStream();

    using (XmlTextWriter xml = new XmlTextWriter(stream, Encoding.UTF8))
    {
        xml.WriteStartDocument();
        xml.WriteStartElement("methodCall");
        xml.WriteElementString("methodName", "weblogUpdates.ping");
        xml.WriteStartElement("params");
        xml.WriteStartElement("param");
        xml.WriteElementString("value", name);
        xml.WriteEndElement();
        xml.WriteStartElement("param");
        xml.WriteElementString("value", url);
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.WriteEndElement();
        xml.Close();
    }

    using (HttpWebResponse response = (HttpWebResponse)request.GetResponse())
    {
        using (StreamReader sr = new StreamReader(response.GetResponseStream()))
        {
            string result = sr.ReadToEnd();
            sr.Close();
        }
        response.Close();
    }
}