Sunday, July 13, 2008

Read RSS with LINQ to XML

 

I'm sure this has been posted somewhere before, but all a quick google turned up was how to read RSS into a data set. So here is a simple way to read RSS using LINQ

 

First an object to hold the results:

 

using System;

[Serializable()]
public class RssItem
{
public string Title { get; set; }

public string Link { get; set; }

public string Description { get; set; }

}
This is really just a simple object no smoke and mirrors, probably could be a simple struct as well.
And the code to read the RSS and populate or object
 

public static List<RssItem> ImportRSS(string source)
{
XDocument doc = XDocument.Load(source,LoadOptions.None);

return (from i in doc.Descendants("channel").Elements("item")
select new RssItem()
{
Title = i.Element("title").Value,
Link = i.Element("link").Value,
Description = i.Element("description").Value
}).ToList();
}

This just loads the RSS into an XDocument object and then we query the descendants to find the items. We place those into the RssItem and return them as a list by using the ToList extension.

And there you have it no more data set.

Friday, July 4, 2008

The eternal debate continues

If you program on the Microsoft stack and unless you live in a cave or just woke from a coma you've surely heard of or even been part of the VB vs C# debate. Visual Studio Magazine has recently rekindled the arguments again. So I thought I'd take a few lines to reiterate my comments to the editor.

I started in development with C++ and MFC which I worked with for a bit over two years, I changed jobs and found myself doing classic asp and VB6. At the time this was pretty much the norm. When .Net was announced I was eager to learn and get onboard with all this new framework had to offer. It looked like things were going to get a lot better for VB in the .Net world and the language would evolve into a first class object oriented language. But even from the earliest days of .Net there was the rumbling of those who didn't want change or evolution. Their mantra was "the same but different". As these things go the squeaky wheel got the grease and VB retained a lot of the baggage it had been carrying around since version 1.

This to me was a sign to look elsewhere in the stack to find the evolution that would make me more productive. The search was to lead me to C#, coming from a, by this time distant C++ background, it was a fairly easy choice. It has been a choice that I have not regretted once. I will no debate the choice of language, because whether it's Java, C++, C# or VB, for many developers it's a near religious choice.

I believe the choice is black and white, evolve or wither, but as long as the "back in version 1" crowd is still making noise, VB will always remain a "could have been" contender.

Wednesday, July 2, 2008

TSQL Debugging in SQL 2008

TSQL Debugging is back in SQL Server 2008, along with intellisense  and code outlining, it's a great productivity enhancement. There at least one major difference between the debugging support in SQL 2000 and SQL 2008 that you should probably be aware of, stored procedure debugging. There is no direct way to step into a stored procedure either from the toolbar or the context menu. Debugging then becomes a two step process for stored procedures

  1. Select the procedure and script as execute
  2. Set a breakpoint on the EXEC statement
  3. Select debug from the menu
  4. When the breakpoint is hit, F11 to step into the stored procedure
  5. You can now step through the TSQL in the stored procedure

This is different then the VS 2008 stored procedure debugging support where you can select step into from the context menu. I would have though they would be pretty close in functionality since it appears that they are using much of the VS debugging functionality in SSIS.

Tuesday, July 1, 2008

SQL 2008 RC0 BIDS fails to install

 

I ran smack into this when installing RC0 with VS 2008 SP1 (beta). It appears that the SQL installer is looking for a product ID that is changed in SP1. There is a work around on the connect web site  in a nut shell though the solution is

  1. Go to Add/Remove Programs
  2. Select Show updates
  3. Remove VS Shell update KB945140
  4. Install SQL BIDS

The only problem I had with this approach is that the uninstall wants a path to the vs_shell.msi from the service pack install. This is typically under Local Settings\Temp\Microsoft Visual Studio 2008 SP1 (Beta) if you're asked during the uninstall.

The other way around this issue is

  1. Install VS 2008
  2. Install .Net 3.5 SP1 (beta)
  3. Install SQL 2008 RC0
  4. Install VS 2008 SP1 (beta)

This works a lot better than uninstalling the KB after the fact, given how well the uninstall's clean up after themselves. Hopefully this issue appears to be marked as fixed on the connect site and RTM should be soon since Q3 is upon us.

Wednesday, October 3, 2007

Microsoft is releasing the .Net 3.5 framework source

Microsoft has announced they will release the source code for the .Net framework libraries with the release of Visual Studio 3.5.

The source will be released under the Microsoft Reference License. In addition an Internet facing symbol server will allow you to grab the debugging symbols from within Visual Studio.

All the details can be read over at ScottGu's blog.

Monday, September 24, 2007

XML Literals in VB 9.0

  I usually live in the C# world, have been since 1.0 of the .Net framework. Back then I was interested in VB.Net until the "Don't change VB" crowd started clamoring, at that point I knew that for at least the time being VB.Net would be crippled so I moved on to C#. I've looked at it from time to time since 1.0, but none of it's features has really stood out from the crowd, until now.

  I've spent a great deal of time looking at an experimenting with the .Net 3.5 changes around LINQ and C# and VB.Net are equally matched, with the exception of XML Literals, which is a VB only feature. So what is an XML Literal? Quite simply it's the ability to embed XML directly into your code. Ok, your saying, "I can do this today, so what's the big deal?", the big deal is the XML is a first class object, not a string! In addition to embedding the XML directly in your code, you can also embed expressions in the form of "<%= [exp] %>", which allows you to populate the XML programmatically.

Here's a VB.Net code snippet that populates an XML Literal using an expression to assign the element values:

Public Function PopulateXml(ByVal boundary As Boolean _
                                            , ByVal endPoint As Boolean _
                                            , ByVal vertex As Boolean) As XElement
       Dim snapSettings As XElement = _
       <SnappingOptions>
           <Boundary><%= boundary.ToString() %></Boundary>
           <Endpoint><%= endPoint.ToString() %></Endpoint>
           <Vertex><%= vertex.ToString() %></Vertex>
       </SnappingOptions>

       Return snapSettings
   End Function

 

The code example above returns a LINQ XElement object, the nodes are populated using the expression defined inside the XML.In the end this allows you to produce XML output that matches the original document format and it's embedded right in the code.

 My hats off to the VB team for this little gem, I hope the C# team pays attention and adds it to C# 4.0.

Monday, September 3, 2007

LINQ and Geocoding

Last week had an opportunity to present an overview of LINQ to my coworkers during a brown bag training session. I ran across this article on LINQ and geocoding by Rob Conery, that provides a interesting look at using LINQ to query a to find hotels that a closer than 100 miles.