【发布时间】:2014-12-01 19:25:08
【问题描述】:
如您所见,我正在尝试获取 yahoo 的天气网络服务。我知道有人问过这个问题很无聊,但我的情况更加具体和复杂
我在解析这个 XML 文档时遇到错误,我正在尝试以下代码:
XmlDocument doc = someXmlWebResult;
XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable);
ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNodeList nodes = doc.SelectNodes("/query/results/weather/rss/channel/item/yweather:forecast", ns);
但 XmlNodeList 节点始终为空。 这是我的 xml 文档...这是一种特定的因为我在这里有多个命名空间,我真的不知道如何获得所需的节点列表
我是 C# 新手,所以请对我放轻松:)
<?xml version="1.0" encoding="UTF-8" ?>
- <query xmlns:yahoo="http://www.yahooapis.com/v1/base.rng" yahoo:count="1" yahoo:created="2014-10-07T10:29:00Z" yahoo:lang="en-US">
- <results>
- <weather xmlns="http://where.yahooapis.com/v1/schema.rng">
- <rss version="2.0" xmlns:geo="http://www.w3.org/2003/01/geo/wgs84_pos#" xmlns:yweather="http://xml.weather.yahoo.com/ns/rss/1.0">
- <channel>
<title>Yahoo! Weather - Tunis, TN</title>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.html</link>
<description>Yahoo! Weather for Tunis, TN</description>
<language>en-us</language>
<lastBuildDate>Tue, 07 Oct 2014 11:00 am CET</lastBuildDate>
<ttl>60</ttl>
<yweather:location city="Tunis" country="Tunisia" region="" />
<yweather:units distance="km" pressure="mb" speed="km/h" temperature="C" />
<yweather:wind chill="27" direction="160" speed="9.66" />
<yweather:atmosphere humidity="48" pressure="1015.92" rising="1" visibility="8" />
<yweather:astronomy sunrise="6:18 am" sunset="5:54 pm" />
- <image>
<title>Yahoo! Weather</title>
<width>142</width>
<height>18</height>
<link>http://weather.yahoo.com</link>
<url>http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif</url>
</image>
- <item>
<title>Conditions for Tunis, TN at 11:00 am CET</title>
<geo:lat>36.8</geo:lat>
<geo:long>10.17</geo:long>
<link>http://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.html</link>
<pubDate>Tue, 07 Oct 2014 11:00 am CET</pubDate>
<yweather:condition code="30" date="Tue, 07 Oct 2014 11:00 am CET" temp="27" text="Partly Cloudy" />
- <description>
- <![CDATA[ <img src="http://l.yimg.com/a/i/us/we/52/30.gif"/><br />
<b>Current Conditions:</b><br />
Partly Cloudy, 27 C<BR />
<BR /><b>Forecast:</b><BR />
Tue - Sunny. High: 29 Low: 19<br />
Wed - Sunny. High: 32 Low: 21<br />
Thu - Sunny. High: 31 Low: 22<br />
Fri - Sunny. High: 29 Low: 21<br />
Sat - Sunny. High: 29 Low: 20<br />
<br />
<a href="http://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.html">Full Forecast at Yahoo! Weather</a><BR/><BR/>
(provided by <a href="http://www.weather.com" >The Weather Channel</a>)<br/>
]]>
</description>
<yweather:forecast code="32" date="7 Oct 2014" day="Tue" high="29" low="19" text="Sunny" />
<yweather:forecast code="32" date="8 Oct 2014" day="Wed" high="32" low="21" text="Sunny" />
<yweather:forecast code="32" date="9 Oct 2014" day="Thu" high="31" low="22" text="Sunny" />
<yweather:forecast code="32" date="10 Oct 2014" day="Fri" high="29" low="21" text="Sunny" />
<yweather:forecast code="32" date="11 Oct 2014" day="Sat" high="29" low="20" text="Sunny" />
<guid isPermaLink="false">TSXX0010_2014_10_11_7_00_CET</guid>
</item>
</channel>
</rss>
</weather>
</results>
</query>
- <!-- total: 31
-->
- <!-- engine9.yql.bf1.yahoo.com
-->
【问题讨论】: