2014-10-07 335 views
1

正如您所看到的,我正在尝试获取yahoo的天气Web服务。我知道,这个问题已经被问孔,但我的情况有点更具体和复杂xmldocument.selectNodes始终为空

在解析这个XMLDOCUMENT我得到一个错误,我想下面的代码:

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文件...这是一种特定东阳我在这里有多个命名空间,我真的不知道怎么去HTE需要的节点列表

我是新来的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 
     --> 

回答

0

意见后编辑答案:

您需要在您的namespacemanager增加一个(见我ns1命名空间),并在查询中指定的命名空间:

// Create a new XmlDocument 
XmlDocument doc = new XmlDocument(); 

// Load data 
doc.LoadXml(
    "<?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>"); 

XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); 
ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0"); 
ns.AddNamespace("ns1", "http://where.yahooapis.com/v1/schema.rng"); 

XmlNodeList nodes = doc.SelectNodes("/query/results/ns1:weather/ns1:rss/ns1:channel/ns1:item/yweather:forecast", ns); 

foreach(XmlNode node in nodes) 
{ 
    Console.WriteLine("{0}: {1}, {2}F - {3}F", 
     node.Attributes["day"].InnerText, 
     node.Attributes["text"].InnerText, 
     node.Attributes["low"].InnerText, 
     node.Attributes["high"].InnerText); 
} 

你可以在这里进行测试:http://rextester.com/WZLNF43949

相似,所以问题:

+0

“/ RSS /渠道/项目/ yweather:预测” 仅适用于在在您指定的示例中,由特定链接“http://xml.weather.yahoo.com/forecastrss?p=94704”配置的xml文件的结构...我的xml结构不同,如果您测试这个我上面提到的xml文件不起作用 – user3744080 2014-10-07 11:41:19

+0

你能给我你打电话来获得结果的请求吗? – 2014-10-07 11:42:34

+0

ofcourse,这里是 https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20weather.bylocation%20WHERE%20location%3D'Tunis'%20AND%20unit%3D %22c%22&format = xml&env = store%3A%2F%2Fdatatables.org%2Falltableswithkeys – user3744080 2014-10-07 11:50:28