【发布时间】:2009-08-21 01:04:21
【问题描述】:
我一直在摸索,试图弄清楚如何使用 C#/XPath/SelectNodes 从 Yahoo API XML 结果中提取节点。
我基本上遇到了与以下所述相同的问题
C# XmlDocument SelectNodes 以及SelectNodes not working on stackoverflow feed下的@
我坚持的一点是准确地理解将什么用作 xmlns,以及在 XPath 中引用节点时为什么/是否需要使用前缀,因为 XML 节点本身没有前缀。 .Net 3.5 不是此项目的选项。
我的代码尝试(几次迭代之一):
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.
以及我正在使用的 XML:
<?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&tt=Pizza&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&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 -->
【问题讨论】: