我正在尝试检索XML文档中单个节点的值。
以下是我尝试过的一些不起作用的方法:
public class Location
{
private String latitude;
public void updateLocation(String woeid)
{
String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);
XmlDocument weatherData = new XmlDocument();
weatherData.Load(query);
XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNamespaceManager man = new XmlNamespaceManager(weatherData.NameTable);
man.AddNamespace("geo:lat", "http://xml.weather.yahoo.com/ns/rss/1.0");
latitude = channel.SelectSingleNode("geo:lat", man).InnerText;
}
这种方法也不起作用:
public class Location
{
private String latitude;
public void updateLocation(String woeid)
{
String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);
XmlDocument weatherData = new XmlDocument();
weatherData.Load(query);
XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
latitude = channel.SelectSingleNode("item").SelectSingleNode("geo:lat").InnerText;
}
}
为什么我无法获得" geo:lat"的价值?
答案 0 :(得分:0)
1)加载方法不会等待,因此它没有任何内容。请参阅HTTPGetXML()函数以获取XML。
2)查看我如何创建命名空间并使用它。
3)使用// yweather:location。
向右跳到所需的节点祝你好运
using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Xml;
using System.Net;
using System.IO;
namespace mblog {
public partial class WebForm1 : System.Web.UI.Page {
public XmlDocument HTTPGetXML(string strURL) {
HttpWebRequest req = (HttpWebRequest)HttpWebRequest.Create(strURL);
req.Method = "GET";
req.Timeout = 120000; // 2 minutes
XmlDocument xmlReturn = new XmlDocument();
// UGH! not set by default!
req.Proxy.Credentials = System.Net.CredentialCache.DefaultCredentials;
// Set AllowWriteStreamBuffering to 'false'.
req.AllowWriteStreamBuffering = false;
HttpWebResponse resp = (HttpWebResponse)req.GetResponse();
string msg;
try {
// Get the response from the server
StreamReader reader = new StreamReader(resp.GetResponseStream());
msg = reader.ReadToEnd();
xmlReturn.LoadXml(msg);
} catch (Exception e) {
throw new Exception("Error posting to server.", e);
}
return xmlReturn;
}
protected void Page_Load(object sender, EventArgs e) {
try {
XmlDocument xmlWeather = HTTPGetXML("http://weather.yahooapis.com/forecastrss?w=2475688");
NameTable nt = new NameTable();
XmlNamespaceManager nsmgr;
nsmgr = new XmlNamespaceManager(nt);
nsmgr.AddNamespace("yweather", "http://xml.weather.yahoo.com/ns/rss/1.0");
XmlNode ndLocation = xmlWeather.SelectSingleNode("//yweather:location", nsmgr);
if( ndLocation != null ) {
string strCity = ndLocation.Attributes["city"].Value;
}
} catch (Exception ex) {
Response.Write( ex.Message );
}
}
}
}
答案 1 :(得分:0)
事实证明我使用了错误的命名空间uri。 XML文档有两个名称空间,一个用于yweather
,另一个用于geo
。
您将在XML文档的顶部找到要使用的命名空间的正确uri。
此代码可让我成功检索geo:lat
:
public void updateLocation(String woeid)
{
String query = String.Format("http://weather.yahooapis.com/forecastrss?w={0}", woeid);
XmlDocument weatherData = new XmlDocument();
weatherData.Load(query);
XmlNode channel = weatherData.SelectSingleNode("rss").SelectSingleNode("channel");
XmlNamespaceManager man = new XmlNamespaceManager(weatherData.NameTable);
man.AddNamespace("geo", "http://www.w3.org/2003/01/geo/wgs84_pos#");
latitude = channel.SelectSingleNode("item").SelectSingleNode("geo:lat", man).InnerText;
}