xmldocument.selectNode始终为空

本文关键字:selectNode xmldocument | 更新日期: 2023-09-27 18:25:51

正如你所看到的,我正在尝试获得雅虎的天气web服务。我知道这个问题被问得很无聊,但我的情况有点具体和复杂

我在解析这个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文档。。。这是一种特定的bea,因为我在这里有多个命名空间,我真的不知道如何获得所需的nodeList

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

xmldocument.selectNode始终为空

评论后编辑的答案:

您需要在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问题:

  • XmlDocument.SelectSingleNode和xmlNamespace问题
  • 可以';无法获取XmlDocument.SelectNode来检索我的任何节点