Generating ATOM Feed

Simple way to generate ATOM feeds.
On wikipedia you can find information about ATOM feeds and see an example of an ATOM feed. I’ll use that example as template and use the StringBuilder to create the feed.

NewsCollection NewsMessages = new NewsCollection();
NewsMessages.GetAllNewsMessages();

string location = "C:\\TEMP\\feed.xml";

string title = "Example Feed";
string subtitle = "A subtitle.";
string link = "http://example.org/";
string self = "http://example.org/feed/";
string id = "60a76c80-d399-11d9-b91C-0003939e0af6";
            
StringBuilder feed = new StringBuilder();

feed.Append("<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n");
feed.Append("<feed xmlns=\"http://www.w3.org/2005/Atom\">\n");
feed.AppendFormat("<title>{0}</title>\n", title);
feed.AppendFormat("<subtitle>{0}</subtitle>\n", subtitle);
feed.AppendFormat("<link href=\"{0}\" rel=\"self\" type=\"application/atom+xml\" />\n", self);
feed.AppendFormat("<link href=\"{0}\" />\n", link);
feed.AppendFormat("<id>urn:uuid:{0}</id>\n", id);
feed.AppendFormat("<updated>{0:s}Z</updated>\n", DateTime.Now.ToUniversalTime());

foreach (News NewsMessage in NewsMessages)
{
    feed.Append("<entry>\n\t");
    feed.AppendFormat("<title>{0}</title>\n\t", NewsMessage.Title);
    feed.AppendFormat("<link href=\"{0}\" />\n\t", NewsMessage.Link);
    feed.AppendFormat("<id>urn:uuid:{0}</id>\n\t", NewsMessage.Id);
    feed.AppendFormat("<updated>{0:s}Z</updated>\n\t", NewsMessage.PublicationDate);
    feed.AppendFormat("<summary>{0}</summary>\n\t", NewsMessage.Text);
    feed.Append("<author>\n\t\t");
    feed.AppendFormat("<name>{0}</name>\n\t\t", NewsMessage.Author.Name);
    feed.AppendFormat("<email>{0}</email>\n\t", NewsMessage.Author.Email);
    feed.Append("</author>\n");
    feed.Append("</entry>\n");
}

feed.Append("</feed>");

using (StreamWriter writer = new StreamWriter(location))
{
    writer.Write(feed.ToString());
}

Objects

public class Author
{
    public string Name { get; set; }
    public string Email { get; set; }
}

public class News
{
    public string Title { get; set; }
    public string Link { get; set; }
    public string Id { get; set; }
    public string Text { get; set; }
    public DateTime PublicationDate { get; set; }
    public Author Author { get; set; }
}

public class NewsCollection : ICollection<News>
{
    private List<News> list = new List<News>();

    public void GetAllNewsMessages()
    {
        Author author = new Author();
        author.Name = "John Doe";
        author.Email = "johndoe@example.com";

        News news1 = new News();
        news1.Author = author;
        news1.Title = "Atom-Powered Robots Run Amok";
        news1.Link = "http://example.org/2003/12/13/atom03";
        news1.Id = "1225c695-cfb8-4ebb-aaaa-80da344efa6a";
        news1.PublicationDate = new DateTime(2003, 12, 13);
        news1.Text = "Some text.";

        News news2 = new News();
        news2.Author = author;
        news2.Title = "Lorem ipsum";
        news2.Link = "http://example.org/2012/10/23/atom03";
        news2.Id = "cd52cde9-bcdd-417a-a1c5-ab6025d87bbf";
        news2.PublicationDate = new DateTime(2012, 10, 23);
        news2.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";

        this.list.Add(news1);
        this.list.Add(news2);
    }

    #region ICollection Methods
    ...
    #endregion
}

Now after generating the ATOM feed, it’s possible to validate the feed.
http://validator.w3.org/appc/check.cgi
When validating the feed right now, you’ll get a warning message
“Self reference doesn’t match document location”.
You can ignore this message. When the feed is hosted on the same location as the self reference, this message will be gone.

Advertisements

Comments are closed.

%d bloggers like this: