解析 XML 数据时出错

发布于 2024-11-27 11:55:50 字数 1152 浏览 0 评论 0原文

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

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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

心的憧憬 2024-12-04 11:55:50

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

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文