C# 解析XML数据时出错
我试图获取谷歌天气数据,如下所示:C# 解析XML数据时出错,c#,xml,linq,linq-to-xml,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); //Pars
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数据传递到解析器中Parse需要一个填充XML的字符串 要么使用XDocument.load(我认为它将采用url),要么使用webrequest获取xml字符串并将其传入