I decided to try consuming a realworld web service as a test of my .Net development skills. Amazon.com's web services toolkit seemed like a good place to start. From the list of available services the WishList seemed the most interesting. After all, how better to get more gifts than to make a core feature of my personal site a direct link to my WishList.
The object model was a bit foreign having a proxy object separate from the request object. It took several good Google searches to find appropriate sample code. What took the most effort in the whole process was troubleshooting a limitation with the System.Data.DataSet's ReadXML method. The XML returned from the Amazon servers included several Artist nodes with title subnodes. The resulting error, " Error creating relationship 'title,'" was a bit confusing and resulted in several wasted hours until I decided to dump the XML into a file on my web server and discovered the problem with the schema.
I hadn't had much experience with the .Net XML classes so it took a few more hours to get the offending nodes scrubbed from the XMLDocument before loading it in the Dataset.
private void LoadWishList(string wishListId, int pageNumber)
{
// create the Web Service (proxy) object
Amazon.AmazonSearchService ws = new Amazon.AmazonSearchService();
// create the request object
Amazon.WishlistRequest request = new Amazon.WishlistRequest();
request.wishlist_id = wishListId;
request.type = "lite";
request.devtag = "XXXXXXXXXXXXXX";
request.page = pageNumber.ToString();
Amazon.ProductInfo info;
try
{
info = ws.WishlistSearchRequest( request );
}
catch( Exception ex )
{
Trace.Warn( ex.ToString() );
return;
}
XmlSerializer serializer = new XmlSerializer( info.Details.GetType() );
// convert the info.Details array to XML and load DataSet
StringWriter writer = new StringWriter();
serializer.Serialize(writer, info.Details );
System.Xml.XmlDocument XmlDoc = new System.Xml.XmlDataDocument();
XmlDoc.LoadXml( writer.ToString() );
//Remove <Artists>'s children. ADO.Net can't load a DataSet with
//multiple DataTables with the same name
foreach (System.Xml.XmlNode node in XmlDoc.SelectNodes("//Artists"))
{
node.RemoveAll();
}
//Serialize the results to a StringReader
System.IO.StringWriter ResultWriter = new StringWriter();
XmlDoc.Save(ResultWriter);
StringReader ResultReader = new StringReader( ResultWriter.ToString() );
dataset = new DataSet();
dataset.ReadXml( ResultReader );
// Bind to the DataList
this.WistListDataList.DataSource = dataset.Tables[0];
this.WistListDataList.DataBind();
}