XPath 与 Yahoo API
我一直在摸索如何使用 C#/XPath/SelectNodes 从 Yahoo API XML 结果中提取节点。
下所述相同的问题
我基本上遇到了与C# XmlDocument SelectNodes 以及SelectNodes not work on stackoverflow feed下
我陷入困境的一点on 是准确理解使用什么作为 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 -->
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&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 -->
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我忍不住说这看起来有点像家庭作业。代码中存在一些错误,给人留下了故意犯错的印象。
GetElementsByTagName()
提供 XPath 表达式?NameTable
?这有效:
I can't help but say that this looks a bit like homework. There are some mistakes in the code that leave the impression of being deliberately made wrong.
GetElementsByTagName()
?NameTable
that has no relation to the actual XML document?This works:
尝试
xml 中的节点没有前缀,但它们确实有 xmlns="..." 指定的命名空间。因此,在执行 XPath 查询时,您需要为要搜索的元素提供名称空间。命名空间管理器和前缀允许您执行此操作。
只需使用一个斜杠,您的查询可能会运行得更快一些。
Try
The nodes in the xml do not have a prefix but they do have a namespace as specified by xmlns="...". Therefore when doing an XPath query you need to provide a namespace for the elements you are searching for. The namespace manager and the prefix allow you to do this.
Your query would probably work a little quicker using just a single slash.