在 WP7 上陷入网络服务困境

发布于 2024-11-24 13:00:08 字数 4014 浏览 4 评论 0原文

昨天我确实询问了如何在 Windows Phone 7 上使用 Web 服务从复杂的 xml 文件中提取一些数据,但不幸的是我没有得到答案,而且我仍然陷入困境。 这是我编写的 C# 代码,但没有在应用程序的屏幕上显示数据:

    public MainPage()
    {
        InitializeComponent();

        SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

        Uri url = new Uri("http://www.google.com/ig/api?weather=paris", UriKind.Absolute);
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(url);
    }


 void client_DownloadStringCompleted(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 areaName = String.Empty;
            string low = String.Empty;
            string high = String.Empty;
            string condition = String.Empty;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {

                        case ("day_of_week"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    day = reader.ReadElementContentAsString();
                                    day = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = day;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("low"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    low = reader.ReadElementContentAsString();
                                    low = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = low;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("high"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    high = reader.ReadElementContentAsString();
                                    high = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = high;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("condition"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    condition = reader.ReadElementContentAsString();
                                    condition = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = condition;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                    }
                }

            }
        }      

    }
}

}

yesterday i did ask about how to extract some data from a complicated xml file , using web service, on the Windows Phone 7 , but unfortuntly i did not get an answer, and i'm still stuck.
This is the c# code i wrote and did not display the data on my application's screen:

    public MainPage()
    {
        InitializeComponent();

        SupportedOrientations = SupportedPageOrientation.Portrait | SupportedPageOrientation.Landscape;

        Uri url = new Uri("http://www.google.com/ig/api?weather=paris", UriKind.Absolute);
        WebClient client = new WebClient();
        client.DownloadStringCompleted += new DownloadStringCompletedEventHandler(client_DownloadStringCompleted);
        client.DownloadStringAsync(url);
    }


 void client_DownloadStringCompleted(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 areaName = String.Empty;
            string low = String.Empty;
            string high = String.Empty;
            string condition = String.Empty;

            while (reader.Read())
            {
                if (reader.NodeType == XmlNodeType.Element)
                {
                    switch (reader.Name)
                    {

                        case ("day_of_week"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    day = reader.ReadElementContentAsString();
                                    day = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = day;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("low"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    low = reader.ReadElementContentAsString();
                                    low = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = low;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("high"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    high = reader.ReadElementContentAsString();
                                    high = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = high;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                        case ("condition"):
                            {
                                if (true == reader.MoveToFirstAttribute())
                                {
                                    reader.MoveToContent();
                                    condition = reader.ReadElementContentAsString();
                                    condition = reader.Value.ToString();
                                    areaItem = new ListBoxItem();
                                    areaItem.Content = condition;
                                    listBox1.Items.Add(areaItem);
                                }

                            } break;
                    }
                }

            }
        }      

    }
}

}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(1

等风来 2024-12-01 13:00:08

您使一切变得过于复杂,它非常简单,使用等等

public class ForecastItem
{
 public string a {get; set;}
 public string b {get; set;}
}

private void forecastReader_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs dc)
    {
        if (dc.Error != null)
        {
            return;
        }
        XElement xmlNews = XElement.Parse(dc.Result);
        listBox1.ItemsSource = from item in xmlNews.Descendants("parent").Elements("sub")
                               select new ForecastItem
                               {
                                   a = item.Element("node").Value,
                                   b = item.Element("title").Value,                                       
                               };
    }

您从这里开始所做的就是将数据绑定到您的 XAML 数据模板

此处文章

You are overcomplicating everything, its really simple, use the

public class ForecastItem
{
 public string a {get; set;}
 public string b {get; set;}
}

and so on

private void forecastReader_DownloadStringCompleted(object sender, DownloadStringCompletedEventArgs dc)
    {
        if (dc.Error != null)
        {
            return;
        }
        XElement xmlNews = XElement.Parse(dc.Result);
        listBox1.ItemsSource = from item in xmlNews.Descendants("parent").Elements("sub")
                               select new ForecastItem
                               {
                                   a = item.Element("node").Value,
                                   b = item.Element("title").Value,                                       
                               };
    }

all you do from here on is Bind the Data to your XAML data template

Article Here

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