Archive for category XML

Syndicating to RSS using the built in .net SyndicationFeed classes

My boss recently showed me a pretty handy bunch of classes for generating RSS and ATOM feeds, all built into dot net.

Start by adding a new reference to your project; System.Services.Web.

The SyndicationFeed class holds all the data about your feed and lets you spit everything out in a number of different formats (RSS2.0, ATOM1.0)- each item in the feed is held in a generic list of SyndicationItem objects. As I work for a publishers, I was creating an RSS feed of coming soon books so I added an extension method to my book class- ToSyndicationItem();

public static SyndicationItem ToSyndicationItem(this Book book)
{
    return new SyndicationItem(book.CoverTitle + " " + book.Subtitle, book.Description, new Uri(book.GetURL()))
    {
        Summary = new TextSyndicationContent(book.DescriptionShort, TextSyndicationContentKind.Plaintext),
        Id = book.Id.ToString(),
        LastUpdatedTime = book.InsertedDate
    };
}

This allows me to use all the standard select methods I already in my book class, to also populate my RSS feed. So to create the feed all I need do in my RSS.ashx handler file is;

List<Book> books = Book.GetBookPublishedBetween(DateTime.Now,DateTime.Now.AddDays( int.Parse( GroupConstants.ComingSoonMaxDays)));
List<SyndicationItem> syndicationItems = new List<SyndicationItem>();
foreach (Book book in books)
    syndicationItems.Add(book.ToSyndicationItem());

feed = new SyndicationFeed(syndicationItems)
{
    Title = new TextSyndicationContent("Coming Soon Titles"),
    Description = new TextSyndicationContent("Forthcoming publications."),
    BaseUri = new Uri(LinkHelper.GetBaseUrl())
};

var output = new StringWriter();
var writer = new XmlTextWriter(output);

new Rss20FeedFormatter(feed).WriteTo(writer);

context.Response.ContentType = "application/rss+xml";
context.Response.Write(output.ToString());

, , ,

No Comments

RSS XSLT

Simple piece of XSL to format an RSS Feed;

<?xml version="1.0" encoding="utf-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
 <xsl:output method="html"></xsl:output>
    <xsl:template match="rss/channel/item">
    <li>
      <a href="{link}" title="{title}">
        <xsl:value-of select="title" />
      </a>
      <p>
        <xsl:value-of select="description" />
      </p>
    </li>
  </xsl:template>
</xsl:stylesheet>

No Comments

Consuming RSS Feeds using ASP.net controls

Real simple example, using an XmlDataSource and my new super best friend, the ListView control;

    <asp:ListView ID="RSSList" runat="server" DataSourceID="RSSData">
        <LayoutTemplate>
            <ul>
                <li id="itemPlaceholder" runat="server" />
            </ul>
        </LayoutTemplate>
        <ItemTemplate>
            <li><h2>
                <%#XPath("title") %>
            </h2>

            <%#XPath("author") %>
            <a href='<%#XPath("link") %>' title=''>View Original Post</a>
            </li>
        </ItemTemplate>
    </asp:ListView>
    <asp:XmlDataSource
        ID="RSSData"
        runat="server"
        DataFile="http://www.shawson.co.uk/codeblog/feed/"
        XPath="rss/channel/item">
    </asp:XmlDataSource>

No Comments

Visually build XPath using “Sketch Path”

Paul found an awesome tool fopr visually parsing XML files and building XPath statements, called Sketch Path available for download here

No Comments

Quick XML serialisation of an object..

Quick code snippet for taking a serialisable object, serialising to XMl then spitting out a string for you to write out to a debug log or something- i used this for spitting back the responses I was getting from a web service call i was making

            StringBuilder sb_xml = new StringBuilder();
            XmlSerializer s = new XmlSerializer( typeof( Hachette.Checkout.Vista.Stock.ProductStockLiteResultResponse ) );
            StringWriter w = new StringWriter(sb_xml);
            s.Serialize(w, ws_response);
            w.Close();
            Response.Write("<!-- Response = " + sb_xml + " -->");

No Comments