首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >使用WP7的Web服务

使用WP7的Web服务
EN

Stack Overflow用户
提问于 2011-07-18 22:07:55
回答 1查看 365关注 0票数 0

我非常感谢您对我上一个问题的帮助,我想知道如何使用web服务从URI读取数据(假设是相同的数据)。这是url链接:http://www.google.com/ig/api?weather=paris我尝试过这段代码,但它不起作用:

代码语言:javascript
复制
 public MainPage()
        {
        InitializeComponent();

        WebClient wc = new WebClient();
        wc.DownloadStringCompleted += new DownloadStringCompletedEventHandler(download_string_complete);
        wc.DownloadStringAsync(new Uri("http://www.google.com/ig/api?weather=hammamet", UriKind.Absolute));
        }

    public void download_string_complete(object sender, DownloadStringCompletedEventArgs e)
        {


       if (e.Error == null)
            {
            ListBoxItem areaItem = null;
            StringReader stream = new StringReader(e.Result);
            XmlReader reader = XmlReader.Create(stream);

            string day = String.Empty;
            string low = String.Empty;
            string high = String.Empty;
            string condition = String.Empty;

            while (reader.Read())
                {
                if (reader.NodeType == XmlNodeType.Element)
                    {
                    if (reader.Name=="forecast_conditions")
                        {
                    WeatherElement welement = new WeatherElement();
                    switch (reader.Name)
                        {
                        case ("day_of_week"):
                                {
                                day = reader.ReadElementContentAsString();
                                areaItem = new ListBoxItem();
                                areaItem.Content = day;
                                friendsBox.Items.Add(day);
                                } break;

                        case ("low"):
                                {
                                low = reader.ReadElementContentAsString();
                                areaItem = new ListBoxItem();
                                areaItem.Content = low;
                                friendsBox.Items.Add(low);
                                } break;

                        case ("high"):
                                {
                                high = reader.ReadElementContentAsString();
                                areaItem = new ListBoxItem();
                                areaItem.Content = high;
                                friendsBox.Items.Add(high);
                                } break;

                        case ("condition"):
                                {
                                condition = reader.ReadElementContentAsString();
                                areaItem = new ListBoxItem();
                                areaItem.Content = condition;
                                friendsBox.Items.Add(condition);
                                } break;
                        }

                    }
                }
            }
        }
    }
EN

回答 1

Stack Overflow用户

回答已采纳

发布于 2011-07-19 21:19:06

代码语言:javascript
复制
  void client_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs e)
    {
        if (e.Error == null)
        {
            StringReader stream = new StringReader(e.Result);
            XmlReader reader = XmlReader.Create(stream);

            string Day = String.Empty;
            string Low = String.Empty;
            string High = String.Empty;
            string ImageUri = String.Empty;
            string Condition = String.Empty;

            reader.MoveToContent();

            while (reader.Read())
            {
                switch (reader.Name)
                {
                    case ("day_of_week"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                              {
                                  Content = reader.GetAttribute("data")
                              });
                            Day = Content.ToString();
                        } break;
                    case ("low"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                            Low = Content.ToString();
                        } break;
                    case ("high"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                            High = Content.ToString();
                        } break;
                    case ("icon"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content =  reader.GetAttribute("data")
                            });
                            Image wkpinImage = new Image();
                            wkpinImage.Source = new System.Windows.Media.Imaging.BitmapImage(new Uri("http://www.google.com" + Content, UriKind.Absolute));
                            wkpinImage.Opacity = 0.8;
                            wkpinImage.Stretch = System.Windows.Media.Stretch.None;
                        } break;

                    case ("condition"):
                        {
                            listBox1.Items.Add(new ListBoxItem()
                            {
                                Content = reader.GetAttribute("data")
                            });
                            Condition = Content.ToString();
                        } break;
                    case("weather"):
                    break;  
                }
            }
        reader.Close(); 
            }
        }
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/6734189

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档