如何在列表框中显示xml值或如何按标签显示。 [英] How to display xml values in list box or how to display by label.?

查看:55
本文介绍了如何在列表框中显示xml值或如何按标签显示。的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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.

推荐答案

不完全确定哪个代码是正确的,因为你必须返回两种不同类型的语句。



但是如果你想获得 XmlDocument的内容您可以使用 InnerXml OuterXml 之一,具体取决于您的需求。



也许阅读一些MSDN文档。

XmlDocument.InnerXml属性 [ ^ ]
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[^]


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;

,这是一个正确的return语句。根据你的话显示错误的陈述永远不会被执行。



sw.ToString()返回字符串。但函数的签名表示该函数返回 XMLDocument ,因此编译器尝试将字符串转换为XMLDocument,这是不可能的。要从字符串中获取XMLDocument,可以使用XMLDocument的 LoadXml 函数。



但是无论如何都没用,因为你已经有了一个XML文档( 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...


这篇关于如何在列表框中显示xml值或如何按标签显示。的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆