views:

64

answers:

2

Hi,

I have the following code where I attempt to make a request to query the yahoo api in order to return the whoid. But I am not able to generate the XML to query it, the error is not displayed.

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;

    }

Is there a better way / easier to generate the XML document from the response?

Many Thanks,

A: 

You can eliminate the step of creating an XmlDocument by passing the XmlTextReader instance to the XDocument.Load() method.

Andy Wilson
+2  A: 

A lot easier than i thought, see 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);
nav