Click here to Skip to main content
15,891,777 members
Please Sign up or sign in to vote.
1.00/5 (1 vote)
See more:
C#
public static XmlDocument WeatherAPI(string sLocation)
    {
        HttpWebRequest WP_Request;
        HttpWebResponse WP_Response = null;
        XmlDocument WP_XMLdoc = null;


        string sKey = "********************"; //The API key generated by World Weather Online
        string sRequestUrl = "http://api.worldweatheronline.com/free/v1/weather.ashx?format=xml&"; //The request URL for XML format

        try
        {
            //Here we are concatenating the parameters
            WP_Request = (HttpWebRequest)WebRequest.Create(string.Format(sRequestUrl + "q=" + sLocation + "&key=" + sKey));
            WP_Request.UserAgent = @"Mozilla/5.0 (Windows; U; Windows NT 6.0; en-US; rv:1.8.1.4) Gecko/20070515 Firefox/2.0.0.4";
            //Making the request
            WP_Response = (HttpWebResponse)WP_Request.GetResponse();
            WP_XMLdoc = new XmlDocument();
            //Assigning the response to our XML object
            WP_XMLdoc.Load(WP_Response.GetResponseStream());
        }
        catch (Exception ex)
        {
            Console.WriteLine(ex.Message);
        }
        WP_Response.Close();
        
        return WP_XMLdoc; // Here we get the five values from the website in xml format. Now I want   those xml values from this "WP_XMLdoc" variable to diplay in textbox or labels.
Posted
Updated 19-Sep-14 2:09am
v4

Not exactly sure which code is correct as you have to return statements with two different types.

But if you want to have the contents of the XmlDocument you can one of the properties InnerXml or OuterXml, depending on what you want.

Maybe read some MSDN documentation.
XmlDocument.InnerXml Property[^]
 
Share this answer
 
v2
return sw.ToString(); // Error shown here.
This means that you did not show us the code you actually use: before that line is a different return statement.
return WP_XMLdoc;
and that's a correct return statement. The statement where according to your words the error is shown will never be executed.

sw.ToString() returns a string. But the signature of the function says that the function returns an XMLDocument, hence the compiler tries to cast the string to XMLDocument, and that is not possible. To get an XMLDocument from a string, you can use the LoadXml function of XMLDocument.

But that's useless anyway, because you already have an XML document (WP_XMLdoc).
You try to accomplish something which you did not tell us - and all the code you showed is so weird because you got confused by not defining your requirements...
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900