好吧所以我试图从他们的YQL获取雅虎财经的信息。下面的代码就是我正在使用的代码。我已经在浏览器中测试了xml位置字符串,它确实返回了xml代码。我正在尝试阅读该代码,解析它和MessageBox我正在寻找的节点。错误是字符串文本为null,我假设这是因为node是null,这可能是因为doc为null。为什么doc为null?
XmlDocument doc = new XmlDocument();
doc.Load("https://query.yahooapis.com/v1/public/yql?q=select%20*%20from%20yahoo.finance.stocks%20where%20symbol%3D%22" + txt_Symbol.Text + "%22&diagnostics=true&env=store%3A%2F%2Fdatatables.org%2Falltableswithkeys");
XmlNode node = doc.DocumentElement.SelectSingleNode("/results/stock/FullTimeEmployees");
string text = node.InnerText;
来自Yahoo YQL的示例响应:
<query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2014-06-03T21:06:06Z" yahoo:lang="en-US">
<diagnostics>
<url execution-start-time="0" execution-stop-time="1" execution-time="1">
<![CDATA[
http://www.datatables.org/yahoo/finance/yahoo.finance.stocks.xml
]]>
</url>
<publiclyCallable>true</publiclyCallable>
<cache execution-start-time="4" execution-stop-time="4" execution-time="0" method="GET" type="MEMCACHED">
<![CDATA[ bcd022d1d39e092a7a1390d6f5cf574a ]]>
</cache>
<cache execution-start-time="4" execution-stop-time="5" execution-time="1" method="GET" type="MEMCACHED">
<![CDATA[ 32c5f8788e8bc68ba5e635ae25257e23 ]]>
</cache>
<cache execution-start-time="5" execution-stop-time="6" execution-time="1" method="GET" type="MEMCACHED">
<![CDATA[ 8c548d6a60dd1f067626b9ecdf556eb3 ]]>
</cache>
<url execution-start-time="5" execution-stop-time="288" execution-time="283">
<![CDATA[ http://finance.yahoo.com/q?s=ibm ]]>
</url>
<url execution-start-time="5" execution-stop-time="288" execution-time="283">
<![CDATA[ http://finance.yahoo.com/q?s=ibm ]]>
</url>
<query execution-start-time="5" execution-stop-time="294" execution-time="289" params="{url=[http://finance.yahoo.com/q?s=ibm]}">
<![CDATA[
select * from html where url=@url and xpath='//div[@id="yfi_investing_head"]/h1 | //div[@class="yfi_quote_summary"]/div[1]'
]]>
</query>
<url execution-start-time="5" execution-stop-time="430" execution-time="425">
<![CDATA[ http://finance.yahoo.com/q/pr?s=ibm ]]>
</url>
<url execution-start-time="5" execution-stop-time="430" execution-time="425">
<![CDATA[ http://finance.yahoo.com/q/pr?s=ibm ]]>
</url>
<url execution-start-time="7" execution-stop-time="455" execution-time="448">
<![CDATA[ http://finance.yahoo.com/q/hp?s=ibm ]]>
</url>
<query execution-start-time="4" execution-stop-time="434" execution-time="430" params="{url=[http://finance.yahoo.com/q/pr?s=ibm]}">
<![CDATA[
select * from html where url=@url and xpath='//table[@class="yfnc_datamodoutline1"]/tr/td/table/tr' limit 4
]]>
</query>
<url execution-start-time="7" execution-stop-time="455" execution-time="448">
<![CDATA[ http://finance.yahoo.com/q/hp?s=ibm ]]>
</url>
<query execution-start-time="6" execution-stop-time="461" execution-time="455" params="{url=[http://finance.yahoo.com/q/hp?s=ibm]}">
<![CDATA[
select * from html where url=@url and xpath='//option[@selected="selected"] | //input[@maxlength="2"] | //input[@maxlength="4"]'
]]>
</query>
<javascript execution-start-time="3" execution-stop-time="462" execution-time="459" instructions-used="46403" table-name="yahoo.finance.stocks"/>
<user-time>464</user-time>
<service-time>2251</service-time>
<build-version>0.2.2525</build-version>
</diagnostics>
<results>
<stock symbol="ibm">
<CompanyName/>
<start>1962-01-02</start>
<end>2014-06-03</end>
<Sector>Technology</Sector>
<Industry>Information Technology Services</Industry>
<FullTimeEmployees>431212</FullTimeEmployees>
</stock>
</results>
</query>
<!-- total: 464 -->
<!-- engine4.yql.bf1.yahoo.com -->
答案 0 :(得分:1)
doc
不为空。但是,node
为null,这是因为您的XPath表达式错误。
您获取的XML文档中的根元素的名称为query
,但只有根元素的名称为/results/stock/FullTimeEmployees
时,您的XPath表达式results
才匹配。由于没有匹配的节点,SelectSingleNode
返回null。
尝试将XPath更改为
/query/results/stock/FullTimeEmployees
或者
//results/stock/FullTimeEmployees
(//results
匹配文档中任何深度的名称为results
的任何元素。)