I have been scratching my head trying to figure out how to use C#/XPath/SelectNodes to extract nodes from a Yahoo API XML result.
I essentially have the same problem as stated under
C# XmlDocument SelectNodes as well as under SelectNodes not working on stackoverflow feed
The point I'm stuck on is understanding exactly what to use as the xmlns, and why/whether I need to use a prefix when referencing the node in XPath as the XML nodes themselves have no prefix. .Net 3.5 is not an option for this project.
My code attempt (one of several iterations):
XmlDocument doc = new XmlDocument();
doc.LoadXml(xml);
XmlNameTable table = new NameTable();
XmlNamespaceManager mgr = new XmlNamespaceManager(table);
mgr.AddNamespace("lcl", "urn:yahoo:lcl");
XmlNodeList nodes = doc.GetElementsByTagName("//lcl:ResultSet/Result");
// !nodes.Count is zero.
and the XML I'm working with:
<?xml version="1.0"?>
<ResultSet xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
xmlns="urn:yahoo:lcl"
xsi:schemaLocation="urn:yahoo:lcl http://local.yahooapis.com/LocalSearchService/V3/LocalSearchResponse.xsd"
totalResultsAvailable="35" totalResultsReturned="1" firstResultPosition="1">
<ResultSetMapUrl>http://maps.yahoo.com/broadband/?q1=San+Diego%2C+CA&amp;tt=Pizza&amp;tp=1</ResultSetMapUrl>
<Result id="20850086"><Title>Filippi's Pizza Grotto</Title><Address>1747 India St</Address>
<City>San Diego</City><State>CA</State><Phone>(619) 232-5094</Phone><Latitude>32.723421</Latitude>
<Longitude>-117.168194</Longitude><Rating><AverageRating>4</AverageRating>
<TotalRatings>115</TotalRatings><TotalReviews>32</TotalReviews><LastReviewDate>1246565979</LastReviewDate>
<LastReviewIntro>... edited ...</ClickUrl>
<MapUrl>http://maps.yahoo.com/maps_result?q1=1747+India+St+San+Diego+CA&amp;gid1=20850086</MapUrl>
<BusinessUrl>http://www.realcheesepizza.com/</BusinessUrl>
<BusinessClickUrl>http://www.realcheesepizza.com/</BusinessClickUrl><Categories><Category id="96926243">Pizza</Category>
<Category id="96926190">Italian Restaurants</Category>
<Category id="96926233">Continental Restaurants</Category>
<Category id="96926234">Carry Out & Take Out</Category><Category id="96926236">Restaurants</Category>
</Categories></Result></ResultSet>
<!-- ws01.ydn.gq1.yahoo.com uncompressed/chunked Thu Aug 20 17:56:17 PDT 2009 -->