【问题标题】:xmldocument.selectNodes always emptyxmldocument.selectNodes 始终为空
【发布时间】: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 
      --> 

【问题讨论】:

    标签: c# xml


    【解决方案1】:

    在 cmets 之后编辑答案:

    您需要在命名空间管理器中再添加一个(请参阅我的 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 问题:

    【讨论】:

    • "/rss/channel/item/yweather:forecast" 仅适用于由您指向我的示例中的特定链接“xml.weather.yahoo.com/forecastrss?p=94704”提供的 xml 文件的结构。 .我的xml结构不同,如果你在我上面提到的xml文档上测试它是行不通的
    • 你能给我你打电话给我的要求吗?
    • 当然,这里是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
    • 我无法通过您的请求获取数据,我收到了Query syntax error(s) [line 1:30 no viable alternative at character '‌',line 1:31 no viable alternative at character '​'] 响应
    • 好的,无论 URL 是什么,我都使用字符串来加载它,并且我接近关于命名空间的解决方案。我想我还需要 5 分钟
    猜你喜欢
    • 2017-07-31
    • 2019-07-18
    • 2019-08-18
    • 2011-12-29
    • 2019-07-02
    • 2019-05-04
    • 1970-01-01
    • 2011-05-04
    • 2015-10-29
    相关资源
    最近更新 更多