解析 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 数据传递到解析器
I was trying to get the google Weather Data as below:
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());
}
I am getting the exception *Data at the root level is invalid. Line 1, position 1. * as I am not passing the XML data but URL. How can I pass the XML data into the parser
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
Parse 需要一个充满 XML 的字符串
要么使用 XDocument.load (我认为这将需要一个 url),要么使用 webrequest 获取 xml 字符串并将其传递进去
Parse is expecting a string filled with XML
Either use XDocument.load (which i think will take a url), or get the xml string using a webrequest and pass that in