OK, even I have to admit this post is so highly targeted to one specific kind of task that most readers won’t be interested in it at all. But since I had to write this code over the weekend, I decided I might as well share it with someone. If you’re working on the .NET platform and you want to upload your products database to Google Base in an XML feed, than this code is for you!
Before I get technical, I’ll explain why you might want to do this. Google Base can be a great way to get your site’s information into the top of Google results, especially if you are selling products in an online shopping cart. Although they’ll accept almost anything – articles, services, even vacation rentals – Google Base works exceptionally well with displaying shopping results for specific product searches. Often it will help a site get into the top 3 results in the SERPs where it may not otherwise appear for ordinary competitive keyword queries. Take a look at the following Shopping Results query for Elmo Live (which I recently purchased for my niece):
If you were selling Elmo Live wouldn’t you like to have your site listed in those top 3 results above fisher-price.com, a site you would probably never be able to knock out of the top spot for that keyword?
In order to submit your product listing into Google Base, you have to first create a data feed in one of a number of formats (Text, XML, etc.). I’ll be creating one in XML format which uses the RSS 2.0 spec. The end result will contain a number of products in the XML file, and each one will look like this:
<title>My Great Widget</title>
<description>Teh best product ever</description>
Basically, the code is pretty simple – all it has to do is loop through a table in SQL Server and format each result set using the System.XML class. Specifically, I’ll be using the XmlTextWriter in VB.NET to create the file. You can download the complete code separately so I’ll just demonstrate a few lines below.
First, we select all the product info from the database:
SELECT productId, productTitle, productDesc, productPrice, ... FROM products
It would be great if it were this simple, but it rarely is. In my live code I actually had to perform several text concatenation functions on my data, and create the search engine-friendly URLs for the link output dynamically. But for the sake of demonstration let’s just say all you have to do is select the data, and write it to XML.
The next part, writing the XML, is equally easy. I just use WriteStartElement to open the <item> tag, and then WriteElementString to create each of the elements, such as <title>. VB.NET makes it very easy to work with XML files using these:
Now when you call the createXMLFeed class from an ASPX page it will display the XML output directly to the browser, so you can simply point the Google data feed upload to this page. If you have many products (more than a few hundred) you’ll probably want to alter the code a bit to actually open a file stream and write the XML directly to a file.
Download Complete VB.NET Code:
ASP.NET Create Google Base XML Data Feed