Recommended

Skip Navigation LinksHome / Articles / View Article

Find Your Books in Amazon.com with Silverlight

+ Add to SilverlightShow Favorites
3 comments   /   posted by Boyan Mihaylov on Sep 28, 2008
(19 votes)
Categories: 18 Aug 08 - 28 Sep 08

Note: This article is submitted by Boyan Mihailov for Silverlight: Write and Win contest. Thanks a lot, Boyan! Hello All, Please drop a comment if you like it.

It is time to vote now! Please, choose your favorite articles and enter your vote by going to contest page. Thank you!

Introduction

Silverlight applications make it easy to perform a lot of actions. One of its greatest capabilities is to work with Web Services. In this article I will demonstrate how you can search books in Amazon.com with Silverlight. I have created a simple application which allows you to navigate through the great catalog of books very easily.

[ Online Demo | Source Code ]

Preparation

Amazon.com provides very functional web service. It allows you to perform lots of actions such as searching items, managing cart operations, etc. In this application I use Amazon Associates Web Service. It is a RESTful one. Access to it is free. You only need to register (for FREE) as a developer and you will get your own access key. You need to provide this key when you make requests to the service.

Application Structure

This image illustrates the simplest structure of this application.  As we use RESTful web service, we do not need to add any web service references to our project. There are lots of libraries specially created for consuming Amazon Web Service (including C# and VB.NET). However, we won't use such a library in this project because its size is big and we do not need the whole functionality, which it provides.

Making Request

When making requests, we just need to send a HTTP request to the web service URL. Thus, I use a helper classes, which help me achieve this. 

When calling the ToString() method of the UrlBuilder class, it returns a string liek this:

http://some_web_address.com/some_page.aspx?q1=v1&q2=v2&....

Parsing Response

Every response from the Amazon Web Service is in XML format, so we can "read" it in many ways. As I am a LINQ fan, we will use LINQ to XML to obtain the response data. XML Response contains a common part (which is common for every request) and an uncommon one. We are going to create an abstract base class, which will parse the common part. After that, we can create other child classes, which derive from our base class, and each of them parse a specific part of the response.

As we want to search only books, we create only a class BookParser, which derives from the ResultParser one. Here is a part from the Parse() method of BookParser.

...
var bookQuerys = from book in XDoc.Descendants("Item")
	let image = book.Element("SmallImage")
	let attr = book.Element("ItemAttributes")
	let price = (attr != null) ? attr.Element("ListPrice") : null
	select new
	{
		BookID = book.Element("ASIN").Value,
		DetailPageURL = book.Element("DetailPageURL").Value,
		ImageUrl = (image != null) ? image.Element("URL").Value : string.Empty,
		ISBN = (attr.Element("ISBN") != null) ? attr.Element("ISBN").Value : string.Empty,
		Title = attr.Element(Title").Value,
		Amount = (price != null) ? Int32.Parse(price.Element("Amount").Value) : 0,
		Price = (price != null) ? price.Element("FormattedPrice").Value : string.Empty,
		Authors = from a in book.Descendants("Author")
			select new
			{
				Name = a.Value
			}
	};
...

User Interface

The main part of the user interface is the search control. It consists of search text box and various search options. When the user presses the Search button, the request is sent to the Amazon Web Service. When the web service sends a response, it is parsed by the corresponding parser class. There is a listbox (which we will modify) which shows the results. We are going to bind a list of Book objects, so here is our listbox item template:

<DataTemplate>
	<Grid>
		<Grid.ColumnDefinitions>
			<ColumnDefinition Width="70"  />
			<ColumnDefinition Width="325"  />
			<ColumnDefinition Width="40"  />
		</Grid.ColumnDefinitions>
		<Image Grid.Column="0" Margin="2" Source="{Binding ImageUrl}"  />
		<StackPanel Orientation="Vertical" Grid.Column="1" Margin="2">
			<TextBlock Text="{Binding Title}" TextWrapping="Wrap" FontWeight="Bold" 
				FontSize="13"  />
			<TextBlock Text="{Binding Authors}" TextWrapping="Wrap"  />
		</StackPanel>
		<Image x:Name="BookDetailsImage" Margin="2" Grid.Column="2" 
			Source="more_info_icon.png" Cursor="Hand" Tag="{Binding DetailPageURL}"  />
	</Grid>
</DataTemplate>

 

TODO

  • Search on every search option change (for example when you choose to search in a specific category)
  • Add a cart support - you will be able to add books to a cart and then just go to Amazon's website to choose a payment model

Summary

Due to its greatest web service support, Silverlight can communicate with every web site, so you can increase your business very efficiently. Using Silverlight to connect to remote stores makes it easy to support product data.

[ Online Demo | Source Code ]

Share


Comments

Comments RSS RSS
  • Very useful  

    posted by Charles Rusch on Sep 29, 2008 06:17

    Thanks for the great article. Keep up the good work!

  • RE: Find Your Books in Amazon.com with Silverlight  

    posted by Oviya on Jan 27, 2009 10:57

    Thank for the article and source code. The Panel Control in the source code does't work. I am new to silverlight and trying to understand from your code.Can you fix that and update the download.

  • RE: Find Your Books in Amazon.com with Silverlight  

    posted by boyanmihailov on Jan 31, 2009 12:25

    Hi Oviya. Actually, I have forgotten to update Amazon Browser to work with Silverlight 2. Now I fixed this, so you can see the online demo or download the updated source code. If you have any further questions, don't hesitate to ask me.

Add Comment

 
 

   
  
  
   
Please add 2 and 8 and type the answer here:

Help us make SilverlightShow even better and win a free t-shirt. Whether you'd like to suggest a change in the structure, content organization, section layout or any other aspect of SilverlightShow appearance - we'd love to hear from you! Need a material (article, tutorial, or other) on a specific topic? Let us know and SilverlightShow content authors will work to have that prepared for you. (hide this)