Search This Blog

Loading...

Saturday, July 25, 2009

Range() and LinqToXml

A couple of days back, I was working on some dashboard components for our portal that use fusion charts. I was generating empty XML templates when I ran into a bit of a challenge: I had to generate 30 "category" nodes that were, basically, dates, and also empty data values for each of those dates. The trick was that the dates had to be based on the day the Chart was accessed.

Well, to be fair, it wasn't really "a challenge", just one of those pain in the ass-spaghetti-stinky piece of code that sometimes we have to write to go on with our lives.

I hated the idea to have some ugly loop (since I discovered linq, all loops are ugly!) that iterated 30 times and grabbed the current date the chart was accessed and then subtracted the index of the loop, and finally made sure that the result was sorted ascending.

So Range function and LinqToXml came to the rescue! Actually, I didn't know that Range() was available in .Net. A co-worker pointed out to me that I did exist and that it was under the "Enumerable" Class. (Thanks Connor!)

So, the potentially "ugly" loop ended up being a beautiful one-liner linq query:

Dim category = (From index In Enumerable.Range(0, 30) Select _
<category label=<%= DateTime.Now.AddDays(-index).ToString("M/dd") %>/>).Reverse

Man, Linq rocks! I can't stop saying it. Well now that I had the hard part done, I had left to generate some empty data sets and then put it all together:

Dim emptySets = From index In Enumerable.Range(0, 30) Select <set value="0"/>

'Build the Xml template
Dim chartData = <?xml version="1.0" encoding="utf-8"?>
<chart>
<categories>
<%= category %>
</categories>
<dataset SeriesName="Name1">
<%= emptySets %>
</dataset>
</chart>

'return the build template to the calling function
Return chartData.ToString

So the ouput of my query would look like this:

<chart>
<categories>
<category label="6/26"/>
<category label="6/27"/>
<category label="6/28"/>
<category label="6/29"/>
<category label="6/30"/>
<category label="7/01"/>
<category label="7/02"/>
<category label="7/03"/>
<category label="7/04"/>
<category label="7/05"/>
<category label="7/06"/>
<category label="7/07"/>
<category label="7/08"/>
<category label="7/09"/>
<category label="7/10"/>
<category label="7/11"/>
<category label="7/12"/>
<category label="7/13"/>
<category label="7/14"/>
<category label="7/15"/>
<category label="7/16"/>
<category label="7/17"/>
<category label="7/18"/>
<category label="7/19"/>
<category label="7/20"/>
<category label="7/21"/>
<category label="7/22"/>
<category label="7/23"/>
<category label="7/24"/>
<category label="7/25"/>
</categories>
<dataset SeriesName="Name1">
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
<set value="0"/>
</dataset>
</chart>

So, there it is. no messy loops, no smelly code, just ....Linq.
Hope this helps

Monday, July 13, 2009

Consuming an RSS feed using LinqToXml

I needed to display some RSS feeds into a dashboard page in one of our websites. I figured I had to mess with XML, XPath, etc.. to browse the xml structure of the post. However, LinqToXml turned out to be the perfect tool for this task. I used Scottgu’s post as a reference, but did my own little twist at the end to avoid another “loop”

First, pass the Rss feed Url into an Xdocument variable:


Dim feedXml As XDocument = _
XDocument.Load(“http://feeds2.feedburner.com/AppriverMalwareWatch”)

Now I need to iterate through the “item” children tags in the XML document and get the “link”, “title” , and “pubDate” elements. Also, I am going to display each post as a hyperlink in an unordered list with the date next to it. Finally, I only need the most recent 8 posts. Here is where magic of LINQ comes in handy:

Dim myFeeds = From feed In feedXml.Descendants("item") Select _
<li>
<a target="_blank" href=<%= feed.Element("link").Value %><%= feed.Element("title").Value %>
</a><%= Date.Parse(feed.Element("pubDate").Value).ToString("M/dd") %>
</li> _
Take 8

'Add xElements to the String literal
Dim myUL = <ul>
<%= myFeeds %>
</ul>

Now that I have added the feeds to my unordered list, I need to write the html to the div container:


'Write string literal to Div's body
Me.uxDivUls.InnerHtml = myUL.ToString

That's it. In a few lines of code I was able to Consume an Rss Feed. What is there not to love about linq to xml??

Happy coding!

Friday, April 18, 2008

Chicken Chicken

Tuesday, April 8, 2008

ClientQueryString Page Property

I was browsing through the Page class in System.Web.UI assembly and I run into the "ClientQueryString" property. Here is its signature:

Public Readonly Property ClientQueryString As String.

It returns the page's querystring in an encoded fashion (so one would have to use

HttpServerUtility.UrlDecode to decode the querystring.) Inspecting the source code I did noticed a couple of interesting things: The property goes through the Page.s_systemPostFields and adds them to a "black list" which will be excluded from the string returned by the property. Also, and I think pretty neat, is the way the black list gets excluded from the query string results: The Request.QueryString gets casted as an HttpValueColletion object and then it invokes its overriden "ToString" Method to which the "blacklist" is passed to in its arguments. (excludeKeys is a Hashtable of "keys to exclude".)
Me._clientQueryString = DirectCast(Me.Request.QueryString, HttpValueCollection).ToString(True, excludeKeys
I thought that was pretty cool, no need to loop through the collection, just pass it as an argument and the .ToString takes care of it.

With this property you can't get an individual querystring value since you can't specify a "key name". You get all of query string values. I guess this is useful if you need to pass on the querystring to another page or if for some reason you need perform some custom parsing and manipulation on the query string.

Saturday, April 5, 2008

Every Build You Break

This is hilarious. Roy Osherove posted a video on YouTube.com where he sang "Every build you break" to a live audience.


Tuesday, March 25, 2008

Visual Studio 2008 HotFix and VB HotFix

A HotFix for VS2088 was released early in February that addresses several issues reported with Web Applications. A list of the fixed bugs can be seen on ScottGu's Blog. You can download the hot fix here.

Also, 2 days ago, the Visual Basic team released a HotFix that solves some performance issues in VS2008. Download it here


Monday, March 24, 2008

App_Offline Goodness

ASP.NET 2.0 comes with this built-in feature to easily bring down an Web Application, make changes to it and reload the application back again.

It's as easy as uploading an App_Offline.htm file in the root of your Web Application directory and you are done. Include any "under construction" or "maintenance" messages in the App_Offline.htm file that you want to display to your end users.

Once you are done, simply delete the file and ASP.NET will reload the application and everything is back to normal. For detailed info check ScottGu's blog.