2.0


3
Feb 06

Amazon, XML, XPath and C# 2.0

I am putting this out there so that, hopefully, no one else will struggle like I just did.

I am trying to use XPath to parse the results from one of the Amazon Web Services. I was not expecting this to be difficult, and it really is not, if you know how to use XPathNavigator. As it turns out, I did not how to use it well.

So first things first… here is an “edited” snippet of the XML response that the Amazon Web Service will return if you are doing a keyword search on “dune”.

[code lang="XML"]





0RBC1MZYXZQMQG6FFPCF








0.110208988189697



True

dune
ItemAttributes
Books


1394
140

0441172717
http://www.amazon.com/...

Frank Herbert
Paperback
9780441172719
25th Anniv
0441172717

799
USD
$7.99

1
535

88
708
55
428

Book
1996-02
ACE Charter
Dune (Dune Chronicles, Book 1)



[/code]

I want to cycle through each of the “ItemAttributes” parsing each and saving their values elsewhere. I initially tried the following:

[code lang="C#"]XPathDocument doc =
new XPathDocument(GetHttpWebResponse(url).GetResponseStream());
XPathNavigator nav = doc.CreateNavigator();
XPathNodeIterator iter =
nav.Select("/ItemSearchResponse/Items/Item/ItemAttributes");

while (iter.MoveNext())
{
// process nodes here...
}[/code]

No matter what XPath expression I would use in the “nav.Select()”, my iterator would be empty.

After several fruitless searches, I finally found this on the MSDN site that showed me what I was doing wrong.

I was in such a hurry to parse the XML response that I totally forgot about the XML Namespaces. Now that I knew what my problem was, it was an easy fix.

[code lang="C#"]XPathDocument doc =
new XPathDocument(GetHttpWebResponse(url).GetResponseStream());
XPathNavigator nav = doc.CreateNavigator();
XmlNamespaceManager manager = new XmlNamespaceManager(nav.NameTable);
manager.AddNamespace("a", "http://webservices.amazon.com/AWSECommerceService/2005-10-05");
XPathNodeIterator iter = nav.Select("/a:ItemSearchResponse/a:Items/a:Item/a:ItemAttributes");

while (iter.MoveNext())
{
// process nodes here...
}[/code]

Another example of why it pays not to be in a hurry and to read the API ;)


27
Jan 06

ClickOnce Deployment

I gave a presentation last night to the Omaha .NET User Group. I think it was received well, and quite a few good questions were asked. One in particular stumped me.

How can I use command line parameters with a ClickOnce deployed application?

Well today I am happy to say that I have found the answer on this post over on Brian Noyes Blog.