你如何从这个xml文件中获取后代元素?

时间:2012-08-27 23:39:29

标签: c# windows-phone-7.1.1

的xml:

http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP?ZIP=48183

这是我的代码:

    private void GetWeather()
    {
        WebClient web = new WebClient();
        web.DownloadStringCompleted += new DownloadStringCompletedEventHandler(web_DownloadStringCompleted);
        string uriAddr = "http://wsf.cdyne.com/WeatherWS/Weather.asmx/GetCityWeatherByZIP?ZIP=48183";
        web.DownloadStringAsync(new Uri(uriAddr));
    }
    void web_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error != null)
        {
            MessageBox.Show("error");

        }

        XElement XmlWeather = XElement.Parse(e.Result);

        foreach (var item in XmlWeather.Descendants("WeatherReturn"))
        {
            // code to get element info
        }

    }

我甚至无法进入foreach声明。有什么建议吗?

1 个答案:

答案 0 :(得分:1)

您需要包含命名空间:

XNamespace ns = "http://ws.cdyne.com/WeatherWS/";

然后,您可以致电XmlWeather.Descendants(ns + "WeatherReturn")