How to read WordPress feeds in a C# MVC .Net application

This is a very easy method you could use to read XML data from a feed and instantiate the object you need with that information. For the following example, I am using an MVC .Net Application code.

First you need to create the Model to store the posts information.

 public partial class BlogPost
 {
    public string Name { get; set; }
    public string URL { get; set; }
    public string Description { get; set; }
    public System.DateTime Date { get; set; }
 }

The following step is used to read the XML data, I have created a helper class with a static method.

public class BlogPostsLookUp
 {
    public static IEnumerable<BlogPost> GetBlogPosts()
    {
      XDocument feedXML = XDocument.Load("http://clarifycoder.com/feed/");

      var feeds = from feed in feedXML.Descendants("item")
            select new BlogPost
            {
              Name = feed.Element("title").Value,
              URL = feed.Element("link").Value,
              Date = DateTime.Parse(feed.Element("pubDate").Value),
              Description = feed.Element("description").Value,
             };

       return feeds;
    }
 }

Once you have the list of blog post you can use it in the View for displaying the information

<ol class="blog-home">
    @foreach (var blog in BlogPostsLookUp.GetBlogPosts())
     {
       <li>
           <a href="@blog.URL" target="_blank">@blog.Name <br /></a>
           <span style="color: #b8c4c4">
               posted on 
               <span style="color:#808080;font-style:italic">
                  @blog.Date.ToString("MMMM dd, yyyy")
               </span>
           </span>
      </li>
    }

</ol>

Sending email in an MVC application

I have implemented emailing in some of the web application I have done in the past, and also one of the recent project. So I thought this could be helpful for some of the developers, so decided to share it.

The process could be divided into 3 simple activities.

  1. Create the partial view, with self contained CSS
  2. Implement a method which would generate a string object after initialising the partial view with its model
  3. Send the mail

Note: The below implementaion was done for a site hosted on GearHost

Here is the method converts the partial view to a string. This requires the reference to the Controller object, the partial view name, and Model object that is required by the view.

partial to string

After creating the view that you need, you can use the same code below to send the email.

emailing

Hope this helps. ūüôā

How to do a select in sql and store values into a variable.

I know this is very simple, but the simplest things we usually forget, so I thought I would just write this and keep it for reference later and hope someone out there would find it useful too.

DECLARE @fromDate datetime;
DeCLARE @toDate datetime;

— select payroll cycle
SELECT StartDate into @fromDate, EndDate into @toDate,
FROM PayrollCycles
WHERE RequestID = 2555

Generating a barcode with ZXing.Net

1. Installing ZXing.Net library.

Using nuget package installer install ZXing.Net libarary. Once installed you should be able to see zxing and zxing.presentation references.

2. Using ZXing library in a c# class. 

string content = barcodeNumber;

IBarcodeWriter writer = new BarcodeWriter
{
Format = BarcodeFormat.EAN_13,
Options= new ZXing.Common.EncodingOptions
{
Height=100,
Width =200,
Margin=2
}
};

Bitmap barcodeBitmap;
var barcode= writer.Write(content);
MemoryStream stream = new MemoryStream();
barcode.Save(stream, ImageFormat.Bmp);