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();

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

string title = "Example Feed";
string subtitle = "A subtitle.";
string link = "";
string self = "";
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=\"\">\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.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.AppendFormat("<name>{0}</name>\n\t\t", NewsMessage.Author.Name);
    feed.AppendFormat("<email>{0}</email>\n\t", NewsMessage.Author.Email);


using (StreamWriter writer = new StreamWriter(location))


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 = "";

        News news1 = new News();
        news1.Author = author;
        news1.Title = "Atom-Powered Robots Run Amok";
        news1.Link = "";
        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 = "";
        news2.Id = "cd52cde9-bcdd-417a-a1c5-ab6025d87bbf";
        news2.PublicationDate = new DateTime(2012, 10, 23);
        news2.Text = "Lorem ipsum dolor sit amet, consectetur adipiscing elit.";


    #region ICollection Methods

Now after generating the ATOM feed, it’s possible to validate the feed.
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.


Comments are closed.

%d bloggers like this: