envyM6
envyM6

Reputation: 1237

Universal Windows App, cant execute doc.LoadXml(string)

I'm trying to retrieve WOIED through an universal windows app using the following code block-

    string woeID;
    private string GetWOEID(string zipCode)
    {
        woeID = "";

        XmlDocument woeidData = new XmlDocument();
        string query = String.Format("http://where.yahooapis.com/v1/places.q('{0}')?appid={1}", zipCode, YahooAPI_ID);

        try
        {
            woeidData.LoadXml(query);

        }
        catch (Exception)
        {
        }

        XmlNodeList kk = woeidData.GetElementsByTagName("woeid");
        if (kk.Count != 0)
        {
            woeID = kk[0].InnerText;
            textBox.Text = "";
            GetWeather(); // Calling getweather method.
            return woeID;


        }
        else
        {
            woeID = "";
            textBox.Text = "";
            return woeID;

        }

    }

where zipCode comes from an textBox input and YahooAPI_ID is my developer key. I need to retrieve the WOEID first to pass it on to GetWeather() method to get the weather report in xml from yahoo. But the problem is after try{woeidData.LoadXml(query);} it always steps into catch (exception){} in debugger. While this code block worked on winform apps before using the code try {woeidData.Load(query)}

Any pointers at what I'm doing wrong will be appreciated!

enter image description here

Upvotes: 0

Views: 162

Answers (2)

envyM6
envyM6

Reputation: 1237

Ok so this is how I did it just in case someone wonders around and stumbled upon this...

private async void button_Click(object sender, RoutedEventArgs e)

{
string url = String.Format("http://url");
            HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url);
            HttpWebResponse response = (HttpWebResponse)await request.GetResponseAsync(); 
            StreamReader reader = new StreamReader(response.GetResponseStream());
            XmlDocument elementdData = new XmlDocument();
            woeidData.Load(reader);
            XmlNodeList element = woeidData.GetElementsByTagName("elementID");
}

Upvotes: 0

tolanj
tolanj

Reputation: 3724

The XmlDocument.LoadXml method is expecting an actually string of xml, https://msdn.microsoft.com/en-us/library/system.xml.xmldocument.load(v=vs.110).aspx, not a uri pointing to a resource, you will need to get the contents of the http call seperately.

You could for example use the XmlDocument.Load method, which takes a stream and use a HttpWebRequest https://msdn.microsoft.com/en-us/library/system.net.httpwebrequest(v=vs.110).aspx and its GetResponse().GetResponseStream() methods to load the xml.

Upvotes: 2

Related Questions