1 min read

Amazon Search in XAML

Paul Kinlan

Paul Kinlan

Lead of Chrome DevRel

I have just been playing with some XAML databinding. I was interested by the Channel 9 Example which creates an RSS reader in just a couple of lines of XAML

I hooked it up to Amazons REST API. I had a couple of problems with it that I will go into later. But what follows is the XAML that I used [also it can be downloaded from my server]:

<StackPanelxmlns="http://schemas.microsoft.com/winfx/avalon/2005" xmlns:x="http://schemas.microsoft.com/winfx/xaml/2005" Margin="10"> <StackPanel.Resources> <XmlDataNamespaceManager x:Key="NS"> <XmlNamespace Prefix="a"Uri="http://webservices.amazon.com/ AWSECommerceService /2005-10-05" /> </XmlDataNamespaceManager> <XmlDataSource x:Key="Amazon" XmlNamespaceManager="{StaticResource NS}"Source="http://webservices.amazon.co.uk/onca/xml?Service=AWSECommerceService&AssociateTag=cnetfra-21& AWSAccessKeyId=[SUBSCRIPTION ID]& SearchIndex=Books& Keywords=AJAX& BrowseNode=71& ResponseGroup=Request,Large,Images& Sort=salesrank& Operation=ItemSearch"/>

<DataTemplate x:Key="TitleTemplate"> <TextBlock TextContent="{Binding XPath=a:ASIN}" /> </DataTemplate> </StackPanel.Resources>

<Label Content="{Binding Source={StaticResource Amazon}, XPath=/a:ItemSearchResponse/a:OperationRequest/ a:HTTPHeaders/a:Header/@Name}" FontSize="24" FontWeight="Bold" />

<DockPanel DataContext="{Binding Source={StaticResource Amazon}, XPath=/a:ItemSearchResponse/a:Items/a:Item}" Height="300px" > <ListBox DockPanel.Dock="Left" Binding.XmlNamespaceManager="{StaticResource NS}"ItemsSource="{Binding}" ItemTemplate="{StaticResource TitleTemplate}" IsSynchronizedWithCurrentItem="True" /> <TextBox Name="Contents" Text="{Binding XPath=a:ItemAttributes/a:Title}" Wrap="True" Width="Auto" /> </DockPanel></StackPanel>

The major problem that I had was that the AMAZON API has a default namespace, therefore to be able to use XPath against the datasource you need to include a NamespaceManager so that the XPath queries are resolved correctly. Once this is done everything is really simple.

Now this example I have created leaves a lot to be desired, but you can see how easy it to create.

Stay in the loop.

I'm trialing a newsletter. Join for monthly insights into web dev, Chrome, and the open web.

alternate_email

Get in touch

Open to chat about Chrome or Web development.

Book a consultation