Wednesday, March 12, 2008

Parsing Amazon Web Services (AWS) with C# and .NET

Parsing Amazon Web Services (AWS) with C# and .NET

Amazon web services are one of the most feature and information-rich sourcesof queryable product data available. They even have user and editorial reviews thatyou can present to your customers to induce them to buy through your site. Thebest part is that it’s free for developers (Get it at Amazon.com).Here I want to present a short tutorial on how to access the data in AWS withC#. It’s not really hard to use at all from .NET, and in fact is quite a biteasier than some of the feeds I have dealt with before (e.g. Apple iTunes feed).There are no namespaces in the nodes to worry about so our job is a bit simpler.
I know using XSLT might be preferable, but someone just might be looking for aDOM-centric way to parse an XML feed.In order to query the Amazon information database, you need an AWSAccessKeyId.In order to get paid for any sales originating from your site, you need anAmazon AssociateTag.So the query URL would look something like this (substitute your AccessKeyId for“12345″ and your AssociateTag for “mytag-20″):
http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=12345&AssociateTag=mytag-20
For this operation we will search for some items in a category (Mario VideoGamesin this case), so we append to the URL:&Operation=ItemSearch&SearchIndex=VideoGames&Keywords=Mario
We also want to know about the items and get some prices and images so we tackthis on to the URL:&ResponseGroup=Request,ItemAttributes,OfferSummary,Images
Lastly, we desire to see some reviews done by customers and editorial staff sowe append Reviews,EditorialReview
So the final URL looks like this:
http://webservices.amazon.com/onca/xml?Service=AWSECommerceService&AWSAccessKeyId=12345&AssociateTag=mytag-20&Operation=ItemSearch&SearchIndex=VideoGames&Keywords=Mario&ResponseGroup=Request,ItemAttributes,OfferSummary,Images,Reviews,EditorialReview
The response is big, so I’ll just deal with the relevant parts here. Let’ssuppose we want to get the following information from the first item in thefeed:-DetailPageURL (The page you direct your users to go to buy the item)-SmallImage (Graphical representation of the product)-Title of the game-LowestNewPrice (lowest price for the item on Amazon)-Text of a customer review of the item
The simplified feed looks like this:



True




http://www.amazon.com/gp.......more stuff



http://ec1.images.... more stuff








$26.90






Bla Bla Bla. This is the greatest game ever.






What to do first? Let’s open the XmlDocument:String strURL = "http://webservices.amazon.com.....";
XmlDocument doc = new XmlDocument();
doc.Load(strURL);
//See if the response is valid - Amazon sends this info in a
//node of the XML tree:
XmlNodeList lstIsValid = doc.GetElementsByTagName("IsValid");
//This is nice because .NET automatically drills down a couple
//of levels to find the "IsValid" node tag
if (lstIsValid.Count > 0 && lstIsValid[0].InnerXml == "True")
{
//InnerXml holds is the actual data from the node
//Ready to party on the data because the response is valid
XmlNodeList lstItems = doc.GetElementsByTagName("Item");
//Also automatically getting info a couple of levels
//deep - sweet
//Get a NodeList of the "Item" nodes - they hold our info
if (lstItems.Count > 0)
{
//could use a foreach loop here to get them all
XmlNode nItem = lstItems[0];
//all children of the first "item" node
foreach (XmlNode nChild in nItem.ChildNodes)
{
//Clunky way to find a child node.
//This is the best I could
//come up with from the MSFT docs.
//Anybody have a better way?
if (nChild.Name == "DetailPageURL") //string compare !
{
String strURL = nChild.InnerXml; //our data !
}
else if (nChild.Name == "SmallImage")
{
//Now we have to look through the children of the
//SmallImage node:
foreach (XmlNode nURLImg in nChild.ChildNodes)
{
if (nURLImg.Name == "URL")
{
String strImage = nChild.InnerXml; //our data !
}
}
}
else if (nChild.Name == "ItemAttributes")
{
foreach (XmlNode nIA in nChild.ChildNodes)
{
//Look through each "ItemAttributes" to find the one
//we want again
if (nIA.Name == "Title")
{
String strTitle = nIA.InnerXml; //our data !
}
}
}
else if (nChild.Name == "OfferSummary")
{
foreach (XmlNode nOS in nChild.ChildNodes)
{
//looking again
if (nOS.Name == "LowestNewPrice")
{
foreach (XmlNode nLNP in nOS.ChildNodes)
{
if (nLNP.Name == "FormattedPrice")
{
String strPrice = nLNP.InnerXml; //our data !
break; //done looking here.
}
}
}
}
}
else if (nChild.Name == "CustomerReviews")
{
foreach (XmlNode nCR in nChild.ChildNodes)
{
if (nCR.Name == "Review")
{
foreach (XmlNode nRev in nCR.ChildNodes)
{
if (nRev.Name == "Content")
{
//Our review text!
String strReview = nRev.InnerXml;
}
}
}
}
}
}
}
}

http://www.svdeals.com/

No comments: