2011-07-29 112 views
2

我試圖讓谷歌的氣象資料如下:得到錯誤而解析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數據傳遞到解析器

回答

2

解析期待充滿XML

一個字符串使用XDocument.load(我認爲這將需要一個URL),或使用的WebRequest獲取XML字符串通過那