C# 如何将XMLDocument类型转换为字符串,以便在标签中显示结果

C# 如何将XMLDocument类型转换为字符串,以便在标签中显示结果,c#,asp.net,xmldocument,weather-api,C#,Asp.net,Xmldocument,Weather Api,我正试图在我的网站上显示来自世界天气在线的天气信息。我使用VS2012和c#来创建这个 我可以通过XMLDocument类型变量“WP_XMLdoc”下的函数从world weather online检索数据 请看下面的代码: public static XmlDocument WeatherAPI(string sLocation) { HttpWebRequest WP_Request; HttpWebResponse WP_Response = null; XmlD

我正试图在我的网站上显示来自世界天气在线的天气信息。我使用VS2012和c#来创建这个

我可以通过XMLDocument类型变量“WP_XMLdoc”下的函数从world weather online检索数据

请看下面的代码:

public static XmlDocument WeatherAPI(string sLocation)
{
    HttpWebRequest WP_Request;
    HttpWebResponse WP_Response = null;
    XmlDocument WP_XMLdoc = null;
    String Value;

    string sKey = "xxxxxxxxxxxxxxxxxxxxxxxxx"; //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;

    }

}
所以,现在我只想从“WP_XMLdoc”变量中获取XML数据,并在标签中显示一些细节,如温度、风速、时间等。 我该怎么做

“WP_XMLdoc”下的XML数据如下所示:

<?xml version="1.0" encoding="UTF-8"?>
<data>
    <request>
        <type>City</type>
        <query>London, United Kingdom</query>
    </request>
    <current_condition>
        <observation_time>04:17 AM</observation_time>
        <temp_C>17</temp_C>
        <temp_F>63</temp_F>
        <weatherCode>143</weatherCode>
        <weatherIconUrl>
            <![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0006_mist.png]]>
        </weatherIconUrl>
        <weatherDesc>
            <![CDATA[Mist]]>
        </weatherDesc>
        <windspeedMiles>0</windspeedMiles>
        <windspeedKmph>0</windspeedKmph>
        <winddirDegree>62</winddirDegree>
        <winddir16Point>ENE</winddir16Point>
        <precipMM>0.0</precipMM>
        <humidity>94</humidity>
        <visibility>2</visibility>
        <pressure>1010</pressure>
        <cloudcover>50</cloudcover>
    </current_condition>
    <weather>
        <date>2014-09-19</date>
        <tempMaxC>28</tempMaxC>
        <tempMaxF>82</tempMaxF>
        <tempMinC>14</tempMinC>
        <tempMinF>57</tempMinF>
        <windspeedMiles>5</windspeedMiles>
        <windspeedKmph>8</windspeedKmph>
        <winddirection>SSE</winddirection>
        <winddir16Point>SSE</winddir16Point>
        <winddirDegree>149</winddirDegree>
        <weatherCode>356</weatherCode>
        <weatherIconUrl>
            <![CDATA[http://cdn.worldweatheronline.net/images/wsymbols01_png_64/wsymbol_0010_heavy_rain_showers.png]]>
        </weatherIconUrl>
        <weatherDesc>
            <![CDATA[Moderate or heavy rain shower]]>
        </weatherDesc>
        <precipMM>8.3</precipMM>
    </weather>
</data>

城市
英国伦敦
上午4:17
17
63
143
0
0
62
烯
0
94
2.
1010
50
2014-09-19
28
82
14
57
5.
8.
上海证券交易所
上海证券交易所
149
356
8.3

请帮忙

试试下面的例子:

var str = @"<your xml here>";
XDocument xdoc = XDocument.Parse(str);
var output = new List<string>();

foreach (var element in xdoc.Element("data").Element("current_condition").Elements())
{
    output.Add(string.Format("{0} : {1}",element.Name, element.Value.ToString()));
}
var str=@”;
XDocument xdoc=XDocument.Parse(str);
var输出=新列表();
foreach(xdoc.element(“数据”).element(“当前条件”).Elements()中的var元素)
{
Add(string.Format(“{0}:{1}”、element.Name、element.Value.ToString());
}

这将遍历当前条件节点的属性,您可以根据需要进行调整以提取所需内容。

好的,根据您在评论中的回答,我认为您需要显示多列数据

最好的选择是使用ADO.net使用
GridView
来填充
XML
数据。这有点容易


看一看

假设您现有的代码成功地将XML数据加载到
XmlDocument
对象,那么我们可以使用传递合适的XPath表达式作为参数来获取XML文档的任何特定部分。例如,要获取
值:

string temp_c = WP_XMLdoc.SelectSingleNode("/data/current_condition/temp_C")
                         .InnerText;
另一种选择是使用较新的XML API,
XDocument
。它有
Load()
方法,其功能类似于
XmlDocument.Load()

使用这种方法,我们可以简单地将
XElement
转换为
string
,以获得其值:

string temp_c = (string)WP_XMLdoc.XPathSelectElement("/data/current_condition/temp_C");

您希望如何在标签中显示它?只要查看XML代码,就可以在每个节点中转储XML或值。我只想在屏幕上显示一些信息,比如/data/current\u condition/temp\u c、/data/current\u condition/windspeedKmph、/data/current\u condition/cloudcover等等。不,不是这样的。。。。我只想显示从XML到所需标签的几个值。我想要像这样的:lblTemp.Text=XMLDoc.GetElementsByTagName.temp\u c lblwind.Text=XMLDoc.GetElementsByTagName.windspeed\u kmph xml值位于WP\u XMLDoc下。是否有任何方法可以从中获取值或将WP_XMLdoc转换为字符串,并从中获取所需值并显示在标签中??
string temp_c = (string)WP_XMLdoc.XPathSelectElement("/data/current_condition/temp_C");