2014-10-07 2 views
1

당신이 볼 수 있듯이 나는 야후의 날씨 웹 서비스를 얻으려고하고있다. 나는이 질문이 구멍을 요청되었다는 것을 알고 있지만 내 경우는 좀 더 구체적이고 복잡이 XMLDocument로 구문 분석하는 동안 오류가 점점 오전xmldocument.selectNodes always empty

, 나는 다음과 같은 코드를 시도하고있다 :

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 
     --> 

답변

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

유사 SO 질문 :

+0

"/ RSS/채널/아이템/yweather는 : 예측"는에서만 작동 특정 링크 "http://xml.weather.yahoo.com/forecastrss?p=94704"에 의해 프로비저닝 된 XML 파일의 구조를 가리켰습니다 ... 내 xml 구조가 다른 경우, 테스트하면 이것에 위에서 언급 한 xml 문서가 작동하지 않습니다. – user3744080

+0

결과를 얻으려는 요청을 저에게 줄 수 있습니까? –

+0

물론, 여기는 https://query.yahooapis.com/v1/public/yql?q=SELECT%20*%20FROM%20weather.bylocation%20WHERE%20 위치 3D'Tunis'%20AND%20unit % 3D 귀하의 요청으로 데이터를 가져올 수 없습니다. 쿼리 문법 오류가 있습니다. [행 1:30 문자로 실행 가능한 대안이 없습니다. ''% 22c % 22 & format = xml & env = 저장 % 3A % 2F % 2Fdatatables.org % 2Falltableswithkeys – user3744080