解析XML数据时获取错误

时间:2011-07-29 16:28:26

标签: c# xml linq linq-to-xml

我试图获取谷歌天气数据如下:

try
        {
            string cityName = txtCityName.Text;
            //Format the google URL with CityName
            string weatherURL = string.Format("http://www.google.com/ig/api?weather={0}", cityName);

            //Parse the XML URL and get the Data 
            var weatherXML = XDocument.Parse(weatherURL);
            var weatherResult = from weatherDetail in weatherXML.Descendants("current_conditions")
                                select new currentWeatherCondition
                                {
                                    condition = ((string)weatherDetail.Element("condition").Attribute("data")).Trim(),
                                    temp = ((string)weatherDetail.Element("temp_c").Attribute("data")).Trim(),
                                    imageURL = ((string)weatherDetail.Element("icon").Attribute("data")).Trim(),

                                };


        }
        catch (Exception err)
        {
            Response.Write(err.Message.ToString());
        }

我收到异常* 根级别的数据无效。第1行,位置1. * 因为我没有传递XML数据而是URL。如何将XML数据传递到解析器

1 个答案:

答案 0 :(得分:2)

Parse期待一个用XML填充的字符串

使用XDocument.load(我认为会使用url),或者使用webrequest获取xml字符串并在

中传递