Code Samples,Search Engine Optimization

Creating an XML Data Feed for Google Base in VB.NET

24 Nov , 2008  

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):

Google Shopping Results via Google Base

If you were selling Elmo Live wouldn’t you like to have your site listed in those top 3 results above, 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>
<g:weight>0.1 lb</g:weight>

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:

objX.WriteElementString("id", myReader("productId").toString())

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.
Text File Download
Download Complete VB.NET Code:
ASP.NET Create Google Base XML Data Feed

, , , , , , , , , , ,

5 Responses

  1. Dennis Handa says:

    Can you write an xml program for a real estate feed

  2. Barry Wise says:

    Certainly can – Google Base accepts service feeds as well as product feeds. There are plenty of real estate feeds on there.

  3. Tal Biber says:

    Hi Barry,
    Thank you for this great article and code.

    Good day from Israel.

    Tal Biber

  4. Hi Barry,

    This is very interesting. If you have a regular wordpress blog , can you use the xml feed that it comes with if each post then has a link to at end to a paypal buy now button? I am trying to figure out a non-techie way to do this!


  5. ITCN says:

    @Deb; Not really. Google Base has specific formatting for their XML feeds.

Comments are closed.