从.NET代码中调用Yahoo Web服务

3
我有以下代码,尝试向Yahoo API发送请求以返回whoid。但是我无法生成XML来查询它,也没有显示错误信息。
private string getWOEID()
{
 string woeID = "";

  String reqUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%3D%22farnborough%2Champshire%2Cuk%22&format=xml";
  HttpWebRequest wr = (HttpWebRequest)WebRequest.Create(reqUrl);
  //load the response into a response object
  WebResponse resp = wr.GetResponse();
  // create a new stream that can be placed into an XmlTextReader
  Stream str = resp.GetResponseStream();
  XmlTextReader reader = new XmlTextReader(str);
  reader.XmlResolver = null;
  // create a new Xml document and loading feed data in to it
  XmlDocument xmldoc = new XmlDocument();
  xmldoc.Load(reader);

  //query the woeid with using linq 
  XDocument doc = XDocument.Parse(xmldoc.ToString());
  woeID = doc.Descendants()
                .Where(element => element.Name == "woeid")
                .FirstOrDefault().Value;
  return woeID;

    }

有没有更好的方法/更容易从响应生成XML文档?

非常感谢。


看起来还不错,但是看看这个:http://support.microsoft.com/kb/301232 - kenny
2个回答

2
比我想象的要容易得多,可以参考http://developer.yahoo.com/dotnet/howto-xml_cs.html
    String reqUrl = "http://query.yahooapis.com/v1/public/yql?q=select%20woeid%20from%20geo.places%20where%20text%3D%22farnborough%2Champshire%2Cuk%22&format=xml";

XmlDocument doc = new XmlDocument();
 doc.Load(reqUrl);

0

通过将XmlTextReader实例传递给XDocument.Load()方法,您可以消除创建XmlDocument的步骤。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接