Using SyndicationFeed class to create and read RSS/Atom feeds

Posted by on Comments (0)

Feeds are one of the most important elements in every website out there. One of the main ways to make your website’s contents shareable is to use these kinds of feeds. It also one of the feature I prefer to add in my entire website projects.

Creating and reading RSS/Atom feeds in .NET Framework have a lot of ways of implementations. Some folks are using XLINQ and some using XmlWriter or XmlReader. One of the most popular ways to create RSS feeds is the following:

protected void Page_Load(object sender, EventArgs e)
{
  // Clear any previous output from the buffer
  Response.Clear();
  Response.ContentType = "text/xml";
  XmlTextWriter feedWriter 
    = new XmlTextWriter(Response.OutputStream, Encoding.UTF8);

  feedWriter.WriteStartDocument();

  // These are RSS Tags
  feedWriter.WriteStartElement("rss");
  feedWriter.WriteAttributeString("version", "2.0");

  feedWriter.WriteStartElement("channel");
  feedWriter.WriteElementString("title", "Daily Coding");
  feedWriter.WriteElementString("link", "http://www.dailycoding.com");
  feedWriter.WriteElementString("description", "Daily Coding");
  feedWriter.WriteElementString("copyright", 
    "Copyright 2008 dailycoding.com. All rights reserved.");
 
  // Get list of 20 most recent posts
  PostList posts = PostList.GetTopPostList(AppGlobals.MainArgs, 20);

  // Write all Posts in the rss feed
  foreach(PostInfo post in posts)
  {
    feedWriter.WriteStartElement("item");
    feedWriter.WriteElementString("title", post.Title);
    feedWriter.WriteElementString("description", post.PostHtml);
    feedWriter.WriteElementString("link", 
      UrlHelper.GetShowPostUrl(this, post.Name));
    feedWriter.WriteElementString("pubDate",
      post.DatePosted.ToString());
    feedWriter.WriteEndElement();
  }

  // Close all open tags tags
  feedWriter.WriteEndElement();
  feedWriter.WriteEndElement();
  feedWriter.WriteEndDocument();  
  feedWriter.Flush();
  feedWriter.Close();

  Response.End();
}

Here’s the original post for the code snippet above: http://www.dailycoding.com/Posts/create_rss_feed_programatically_from_data_in_c.aspx

But maybe we should change the way we create and read these feeds.

In .NET Framework 3.5, Microsoft introduced a new class named SyndicationFeed! This class is in System.ServiceModel namespace so you should add a reference to this assembly in order to make use of SyndicationFeed class.

SyndicationFeed Represents a top-level feed object, <feed> in Atom 1.0 and <rss> in RSS 2.0.

Using SyndicationFeed:

In order to get data from a RSS link, you just need to create an instance of SyndicationFeed class.

If you want to create a new feed to publish, just create a default instance of the class; otherwise, you need to use SyndicationFeed.Load static method to load data from a link:

When the SyndicationFeed object filled, you can make use of its properties and methods to get any kind of data you want from specified feed. Here’s an example of creating a list that represents last 5 RSS feed items in a page:

SyndicationFeed feedReader = 
SyndicationFeed.Load("http://ramezanpour.net/syndication.axd"); @foreach (var item in feedReader.GetFeed().Items.Take(5)) { <div> <strong>
<a href="@item.Links[0].Uri.OriginalString">@item.Title.Text</a></strong> <p>@Html.Raw(item.Summary.Text)</p> </div> }

Hope it helps.

Manage your users with the new ASP.NET 3.5 Personalization feature – Part 1

Posted by on Comments (0)

Nearly all developers who’re developing web applications have been working with sessions, cookies, etc.

For example, when you want to authenticate your users you store you user’s authentication information in sessions and cookies. Previously, I posted about ASP.NET Form Authentication which most of ASP.NET developer are working with it because it’s one of the most stable ways for user authentications.

ASP.NET 3.5 provides a new feature named “Personalization” that has very flexible infrastructure enables you to store user’s information in server not client (Cookies store information on client computers). In this post, I’m going to show you how you can use profiles and I’m sure you’ll love it.

ASP.NET has a deep focus on web.config file. You can do lots of things in your configuration file. In order to work with profiles you need to let ASP.NET what user data you want to store. So you have to add some elements to you web.config file:

<system.web>
 
    <profile>
        <properties>
            <add name="FirstName"/>
            <add name="LastName"/>
            <add name="Age" type="System.Int32"/>
        </properties>
    </profile>
    
</system.web>

Web.Config file properties are accessible in code-behind - Photo taken by myselfAs you can see in the code, you determine what properties your profile should have. You can add them very easily and Visual Studio IntelliSense will help you too.

When you added your custom properties you can see them in code-behind so you can get or set you profile information.

Notice that “Age” property in the above code snippet, I just assigned a type attribute for it. It means that you can also specify the type of a property for example:

· string: System.String

· bool: System.Boolean

· int: System.Int32

· …

You can also assign your custom type. For example, imaging you want to store user’s cart information. You can store information in a class and then set the type of your profile property to that class. Here is an example:

<Serializable()> _
Public Class Cart
 
    Private _ProductName As String
    Private _Price As Decimal
    Private _Quantity As Int16
 
    Public Property ProductName() As String
        Get
            Return _ProductName
        End Get
        Set(ByVal value As String)
            _ProductName = value
        End Set
    End Property
 
    Public Property Price() As Decimal
        Get
            Return _Price
        End Get
        Set(ByVal value As Decimal)
            _Price = value
        End Set
    End Property
 
    Public Property Quantity() As Int16
        Get
            Return _Quantity
        End Get
        Set(ByVal value As Int16)
            _Quantity = value
        End Set
    End Property
 
End Class

Note that the class must be Serializable.

Now you can add your property with your custom type:

<add name="Cart" type="Cart" serializeAs="Binary" />

You can also provide default values for your properties. It means if you leave your property with no value, a default value will be replace with null:

<add name="Active" type="System.Boolean" defaultValue="false"/>

It possible to make your properties read-only:

<add name="Active" type="System.Boolean" readOnly="true"/>

I the next part, I’ll show you how to store profile information in a data source such as SQL Server, XML, etc.

Visual Basic 2010 new features

Posted by on Comments (11)

Channel 9 Logo - Photo taken from: http://mschnlnine.vo.llnwd.net/d1/Dev/App_Themes/C9/images/feedimage.pngWorking with new technology is always interesting. Since Visual Studio 2010 and .NET Framework 4 have been announced, most of developers who are developing with Visual Basic start searching about Visual Basic 10 new features.

About a month ago, Lucian Wischik and Lisa Feigenbaum described about new features in Visual Basic 2010. The show is available for download at MSDN Channel 9 website.

You can check it out by click here.

This is my recommendation: View it!