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

时间:2014-09-19 04:33:09

标签: c# asp.net xmldocument weather-api

我试图通过网上世界天气在我的网站上显示天气信息。我使用VS2012和c#来创建它。

我能够从世界天气在线检索数据到XMLDocument类型变量" WP_XMLdoc"。

下的函数。

看看下面的代码:

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数据。变量并在我的标签中显示一些细节,如temp_c,windspeed,time等。 我怎么能这样做?

停留在" 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>

请帮忙!

3 个答案:

答案 0 :(得分:1)

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

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

另一个选择是使用更新的XML API XDocument。它具有Load()方法,其功能类似于XmlDocument.Load()

XDocument WP_XMLdoc = XDocument.Load(WP_Response.GetResponseStream());

使用这种方法,我们可以简单地将XElement投射到string以获得它的价值:

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

答案 1 :(得分:0)

尝试这样的事情,例如:

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()));
}

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

答案 2 :(得分:0)

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

最佳选择是使用GridView使用ADO.net填充XML数据。这很容易。

查看this SO thread