Archive for September, 2010

Reading LendingClub Data in C#

The micro-finance website LendingClub.com lets you download a snapshot of their loan data for the purposes of analysis … which is really cool, and not really a thing that financial companies are prone to do. If this is the kind of thing that interests you, I just wanted to show just how easy it was to read and parse this file

string filepath = @"c:\users\joel\downloads\loanstats.xml";

XDocument xml = XDocument.Load(filepath);
var loansElements = xml.XPathSelectElements("//loan");
var loans = loansElements.Select(x => new
    {
        Id = x.Attribute("id").Value,
        AmountRequested = x.Attribute("amount-requested").Value,
        InterestRate = x.Attribute("interest-rate").Value
        // and so on
    });

foreach (var loan in loans)
{
    Console.WriteLine("{0} - {1} @ {2}", loan.Id, loan.AmountRequested, loan.InterestRate);
}

They also have each data file available as a CSV if you prefer to do things that way.

Comments (2)

Windows Phone 7 WebHelper

Just a quick post, this is a nice little helper class I wrote to simplify doing HTTP get requests in windows phone 7 applications. Not that it’s very difficult, but this hides some of the unnecessary details involved in setting up the asynchronous web request and gives you a super simple API that is easy to understand.

public static class WebHelper
{
    public static void Get(string url, Action<string> action)
    {
        Get(new Uri(url), action);
    }

    public static void Get(Uri uri, Action<string> action)
    {
        var request = WebRequest.CreateHttp(uri);

        request.BeginGetResponse(i =>
        {
            var response = request.EndGetResponse(i);
            var sreader = new StreamReader(response.GetResponseStream());
            var result = sreader.ReadToEnd();
            action(result);
        }, null);
    }
}

Using this class could not be easier:

string u = @"http://bing.com";
WebHelper.Get(u, html =>
    {
        Debug.WriteLine(html);
    });

This class can (and will) be expanded for things like posting form values … I’ll update this post if I do any of those updates :-)

Comments