Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/xml/14.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# xmldocument.selectNodes始终为空_C#_Xml - Fatal编程技术网

C# xmldocument.selectNodes始终为空

C# xmldocument.selectNodes始终为空,c#,xml,C#,Xml,正如你所看到的,我正在尝试获取雅虎的天气网络服务。我知道有人问过这个问题,但我的情况更为具体和复杂 解析此XMLdocument时出错,我正在尝试以下代码: XmlDocument doc = someXmlWebResult; XmlNamespaceManager ns = new XmlNamespaceManager(doc.NameTable); ns.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0")

正如你所看到的,我正在尝试获取雅虎的天气网络服务。我知道有人问过这个问题,但我的情况更为具体和复杂

解析此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文档。。。这是一种特殊的原因,因为我这里有多个名称空间,我真的不知道如何获得所需的节点列表

我是c#的新手,所以请对我放松点:)


- 
- 
- 
- 
- 
雅虎!田纳西州突尼斯天气预报
http://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.html 
雅虎!TN突尼斯天气预报
恩美
2014年10月7日星期二上午11:00 CET
60
- 
雅虎!天气
142
18
http://weather.yahoo.com 
http://l.yimg.com/a/i/brand/purplelogo//uh/us/news-wea.gif 
- 
美国东部时间上午11:00,田纳西州突尼斯的情况
36.8
10.17
http://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.html 
2014年10月7日星期二上午11:00 CET
- 
- 
当前情况:
部分多云,27摄氏度

预测:
星期二阳光明媚。高:29低:19
星期三阳光明媚。高:32低:21
星期四晴。高:31低:22
星期五晴。高:29低:21
周六阳光明媚。高:29低:20



(提供人)
]]> TSXX0010_2014_10_11_7_00_CET - -
编辑评论后的答案:

您需要在namespacemanager中再添加一个名称空间(请参见my
ns1
namespace),并在查询中指定名称空间:

// 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);  
}
//创建一个新的XmlDocument
XmlDocument doc=新的XmlDocument();
//加载数据
doc.LoadXml(
“雅虎天气-突尼斯,TNhttp://us.rd.yahoo.com/dailynews/rss/weather/Tunis__TN/*http://weather.yahoo.com/forecast/TSXX0010_c.htmlYahoo!2014年10月7日上午11:00 CET60Yahoo!Weather14218突尼斯天气http://weather.yahoo.comhttp://l.yimg.com/a/i/brand/purplelogo//uh/us/news-美国东部时间36.810.17htt上午11:00,田纳西州突尼斯的天气状况p://us.rd.yahoo.com/dailynews/rss/weather/Tunis\uuu TN/*http://weather.yahoo.com/forecast/TSXX0010_c.htmlTue,2014年10月7日上午11:00 CET当前情况:
部分多云,27摄氏度

预测:
周二晴。高:29低:19
周三晴。高:32低:21
周四晴。高:31低:22
周五晴。高:29低:21
周六晴高:29低:20



(由提供人提供)
]>TSXX0010_2014_10_11_7_00_CET); XmlNamespaceManager ns=新的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节点) { WriteLine(“{0}:{1},{2}F-{3}F”, node.Attributes[“day”].InnerText, node.Attributes[“text”].InnerText, node.Attributes[“low”].InnerText, node.Attributes[“high”].InnerText); }
您可以在此处进行测试:

类似问题:


“/rss/channel/item/yweather:forecast”仅适用于由您指给我的示例中的特定链接“”提供的xml文件的结构。。。我的xml结构是不同的,如果你在我上面提到的xml文档上测试它,它将不起作用。你能给我你调用的请求以获得结果吗?当然,这里是来自%20weather的*%20。通过位置%20,其中%20location%3D'Tunis“%20和%20unit%3D%22c%22&format=xml&env=store%3A%2F%2Fdatatables.org%2FallTableswithKeys我无法通过您的请求获取数据,我得到了一个
查询语法错误[第1:30行字符处没有可行的替代方法]‌',第1行:31角色位置没有可行的替代方案​']
responseOk无论URL是什么,我都使用字符串来加载它,我已经接近解决方案了,这是关于名称空间的。我想我还需要5分钟
// 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);  
}