window phone7.1 天气预报教程(三)xml读取数据与Isolatedstorage存储数据

          我们已经可以通过在线访问中央气象局取得天气情况了.可是我们不能老时时去取啊.这样流量的费用就太高了.而且如果没有连接上网络,就什么信息都没有了.我们知道,天气的情况一般来说,一天更新一次,基本上不会在什么太大的突然之间的变化.所以我们应该把取到的天气情况存下来,在没有网络的情况下,也可以知道最近几天的的天气.因为我们取一次,就可以显示今后五天的天气吗.这里我们就用到了Isolatedstorage来存储数据,Isolatedstorage是window phone7中用来进行本地存储的类,它只是一个临时性的,这个临时性什么意思呢,就是当你卸载这个软件的时候,它所占用的空间也就同时消失了.里面的东西也跟着被删除了.最大可以是2G,这个不正是我们需求的吗.当用户卸载掉这个软件的时候,我们还要保留这些天气情况干什么呢.好了.我们来看一下如何编写代码:

第一步:当用户第一次使用这个软件时,我们要建立一个xml文件用来存取一个默认的天气情况

/// <summary>
        /// 数据第一次加载时,建立数据文件
        /// </summary>
        public void CreateXml()
        {
            if (iso.FileExists("weatherData.xml"))
            {
                ReadXml();
            }
            else
            {
                IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("weatherData.xml", FileMode.Create, iso);
               
                XElement xe = new XElement("weatherInfo",
                    new XElement("city", "哈尔滨"),
                    new XElement("cityid", "101050101"),
                    new XElement("date_y", "2012年6月13日"),
                    new XElement("week", "星期三"),
                    new XElement("temp1", "24℃~13℃"),
                    new XElement("temp2", "22℃~15℃"),
                    new XElement("temp3", "22℃~15℃"),
                    new XElement("temp4", "26℃~16℃"),
                    new XElement("temp5", "28℃~16℃"),
                    new XElement("temp6", "29℃~16℃"),
                    new XElement("weather1", "雷阵雨"),
                    new XElement("weather2", "小雨转中雨"),
                    new XElement("weather3", "中到大雨转雷阵雨"),
                    new XElement("weather4", "雷阵雨"),
                    new XElement("weather5", "多云转阵雨"),
                    new XElement("weather6", "晴"),
                    new XElement("wind1", "东风3-4级转小于3级"),
                    new XElement("wind2", "东风3-4级转小于3级"),
                    new XElement("wind3", "西南风3-4级转小于3级"),
                    new XElement("wind4", "西南风3-4级转小于3级"),
                    new XElement("wind5", "西南风3-4级转小于3级"),
                    new XElement("wind6", "西南风3-4级转小于3级"),
                    new XElement("info", "较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"));

                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XComment("天气数据"), new XElement("root", xe));

                StreamWriter sw = new StreamWriter(isfs);
                xdoc.Save(sw);
                sw.Close();

                ReadXml();
            }
        }

第二步:建立完了文件,我们就应该对这个建立完的数据进行读取

  /// <summary>
        /// 从XML文件中读取数据
        /// </summary>
        public void ReadXml()
        {
            if (!iso.FileExists("weatherData.xml"))
            {
                MessageBox.Show("文件不存在!");
                return;
            }
            else
            {
                IsolatedStorageFileStream isf = new IsolatedStorageFileStream("weatherData.xml", FileMode.Open, FileAccess.Read, iso);
                StreamReader sr = new StreamReader(isf);
                               
                XElement xDoc = XElement.Parse(sr.ReadToEnd().Trim());

                var xdata = from wd in xDoc.Descendants("weatherInfo")                         
                            select new
                            {
                                city = wd.Element("city").Value,
                                cityid = wd.Element("cityid").Value,
                                date_y = wd.Element("date_y").Value,
                                week = wd.Element("week").Value,
                                temp1 = wd.Element("temp1").Value,
                                temp2 = wd.Element("temp2").Value,
                                temp3 = wd.Element("temp3").Value,
                                temp4 = wd.Element("temp4").Value,
                                temp5 = wd.Element("temp5").Value,
                                temp6 = wd.Element("temp6").Value,
                                weather1 = wd.Element("weather1").Value,
                                weather2 = wd.Element("weather2").Value,
                                weather3 = wd.Element("weather3").Value,
                                weather4 = wd.Element("weather4").Value,
                                weather5 = wd.Element("weather5").Value,
                                weather6 = wd.Element("weather6").Value,
                                wind1 = wd.Element("wind1").Value,
                                wind2 = wd.Element("wind2").Value,
                                wind3 = wd.Element("wind3").Value,
                                wind4 = wd.Element("wind4").Value,
                                wind5 = wd.Element("wind5").Value,
                                wind6 = wd.Element("wind6").Value,
                                info = wd.Element("info").Value
                            };

                

                foreach (var item in xdata)
                {
                    wi = new WeatherInfo
                    {
                        city = item.city,
                        cityid = item.cityid,
                        date_y = item.date_y,
                        week = item.week,
                        temp1 = item.temp1,
                        temp2 = item.temp2,
                        temp3 = item.temp3,
                        temp4 = item.temp4,
                        temp5 = item.temp5,
                        temp6 = item.temp6,
                        weather1 = item.weather1,
                        weather2 = item.weather2,
                        weather3 = item.weather3,
                        weather4 = item.weather4,
                        weather5 = item.weather5,
                        weather6 = item.weather6,
                        wind1 = item.wind1,
                        wind2 = item.wind2,
                        wind3 = item.wind3,
                        wind4 = item.wind4,
                        wind5 = item.wind5,
                        wind6 = item.wind6,
                        info = item.info
                    };
                    selCityid = item.cityid;
                    
                }             

                sr.Close();
                isf.Close();

                Update();
            }
        }

Update()这个方法,就是上节中对前台控制地蚝更新的代码

第三步:更新现有的天气情况,这里有一个问题,就是在我使用linq to xml进行更新的时候,发现它没有修改原有的信息内容,而是在原来的信息下面添加了我修改的内容,这让我很不解.所以没有法子,我只能采用一个最笨的方法,就是在更新天气的时候,我把原来的删除,重新再建立一个.

 /// <summary>
        /// 更新XML文件,取得最新的数据
        /// </summary>
        /// <param name="json"></param>
        public void UpdateXML(JObject json)
        {         
            if (iso.FileExists("weatherData.xml"))
            {
                iso.DeleteFile("weatherData.xml");


                IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("weatherData.xml", FileMode.Create, iso);

                XElement xe = new XElement("weatherInfo",
                    new XElement("city", (string)json["weatherinfo"]["city"]),
                    new XElement("cityid", (string)json["weatherinfo"]["cityid"]),
                    new XElement("date_y", (string)json["weatherinfo"]["date_y"]),
                    new XElement("week", (string)json["weatherinfo"]["week"]),
                    new XElement("temp1", (string)json["weatherinfo"]["temp1"]),
                    new XElement("temp2", (string)json["weatherinfo"]["temp2"]),
                    new XElement("temp3", (string)json["weatherinfo"]["temp3"]),
                    new XElement("temp4", (string)json["weatherinfo"]["temp4"]),
                    new XElement("temp5", (string)json["weatherinfo"]["temp5"]),
                    new XElement("temp6", (string)json["weatherinfo"]["temp6"]),
                    new XElement("weather1", (string)json["weatherinfo"]["weather1"]),
                    new XElement("weather2", (string)json["weatherinfo"]["weather2"]),
                    new XElement("weather3", (string)json["weatherinfo"]["weather3"]),
                    new XElement("weather4", (string)json["weatherinfo"]["weather4"]),
                    new XElement("weather5", (string)json["weatherinfo"]["weather5"]),
                    new XElement("weather6", (string)json["weatherinfo"]["weather6"]),
                    new XElement("wind1", (string)json["weatherinfo"]["wind1"]),
                    new XElement("wind2", (string)json["weatherinfo"]["wind2"]),
                    new XElement("wind3", (string)json["weatherinfo"]["wind3"]),
                    new XElement("wind4", (string)json["weatherinfo"]["wind4"]),
                    new XElement("wind5", (string)json["weatherinfo"]["wind5"]),
                    new XElement("wind6", (string)json["weatherinfo"]["wind6"]),
                    new XElement("info", (string)json["weatherinfo"]["index48_d"]));

                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XComment("天气数据"), new XElement("root", xe));

                StreamWriter sw = new StreamWriter(isfs);
                xdoc.Save(sw);
                sw.Close();
                isfs.Close();
            }
            ReadXml();
        }

OK相应的方法,我们都已经编写完了.现在就把它进行组合一下吧.

第四步,在load方法,我们进处一下处理.这里有一个问题,如果你的手机没有连上网络怎么办呢,我们应该给出一个提示框啊.所以我们就在前台做一个

<Border Background="White" Visibility="Collapsed" x:Name="Netmsg" Grid.ColumnSpan="2" BorderThickness="5" Grid.RowSpan="3" Grid.Column="0" Grid.Row="1">
                <Grid ShowGridLines="True" Background="MediumSlateBlue" Margin="3,3,3,3">
                    <Grid.ColumnDefinitions>
                        <ColumnDefinition Width="456*"></ColumnDefinition>
                    </Grid.ColumnDefinitions>
                    <Grid.RowDefinitions>
                        <RowDefinition Height="80"></RowDefinition>
                        <RowDefinition Height="*"></RowDefinition>
                        <RowDefinition Height="80" />        
                    </Grid.RowDefinitions>
                    <StackPanel Orientation="Horizontal"  Grid.Column="0" Grid.Row="0" VerticalAlignment="Center" HorizontalAlignment="Center">
                        <Image Source="ApplicationIconbak.png" Width="62" Height="62" ></Image>
                        <TextBlock Margin="10" Text="无名天气提醒您" FontSize="32"></TextBlock>
                    </StackPanel>
                    <TextBlock  FontSize="28" Grid.Column="0" Grid.Row="1" HorizontalAlignment="Center" VerticalAlignment="Center">
                        抱歉,由于网络连接问题,无法<LineBreak/>为您更新天气数据,请检查您的<LineBreak/>网络连接或稍后再试。
                    </TextBlock>
                    <Button x:Name="btnNet" Content="确定" Grid.Column="0" Grid.Row="2" Width="200" Click="btnNet_Click"></Button>
                </Grid>
            </Border>

当网络连接不上的时候,它就会显示不出,提示用户连接网络

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)
        {
            
                ReadXml();
                CreateCityidXMl(selCityid);
                if (NetworkInterface.GetIsNetworkAvailable())
                {
                    webclient(selCityid + ".html");
                }
                else
                {
                    this.Netmsg.Visibility = Visibility.Visible;
                }
    
           
        }


运行一下试试吧,你会发现,第一次运行的时候,总会提示没有文件,这怎么办呢.我们都知道,在app.xaml文件中有四个方法,我们来编写一点代码,在其中的Launching中.它是表示当程序运行时进行处理.

private void Application_Launching(object sender, LaunchingEventArgs e)
        {
            if (iso.FileExists("weatherData.xml"))
            {
                //ReadXml();

            }
            else
            {
                IsolatedStorageFileStream isfs = new IsolatedStorageFileStream("weatherData.xml", FileMode.Create, iso);

                XElement xe = new XElement("weatherInfo",
                    new XElement("city", "哈尔滨"),
                    new XElement("cityid", "101050101"),
                    new XElement("date_y", "2012年6月13日"),
                    new XElement("week", "星期三"),
                    new XElement("temp1", "24℃~13℃"),
                    new XElement("temp2", "22℃~15℃"),
                    new XElement("temp3", "22℃~15℃"),
                    new XElement("temp4", "26℃~16℃"),
                    new XElement("temp5", "28℃~16℃"),
                    new XElement("temp6", "29℃~16℃"),
                    new XElement("weather1", "雷阵雨"),
                    new XElement("weather2", "小雨转中雨"),
                    new XElement("weather3", "中到大雨转雷阵雨"),
                    new XElement("weather4", "雷阵雨"),
                    new XElement("weather5", "多云转阵雨"),
                    new XElement("weather6", "晴"),
                    new XElement("wind1", "东风3-4级转小于3级"),
                    new XElement("wind2", "东风3-4级转小于3级"),
                    new XElement("wind3", "西南风3-4级转小于3级"),
                    new XElement("wind4", "西南风3-4级转小于3级"),
                    new XElement("wind5", "西南风3-4级转小于3级"),
                    new XElement("wind6", "西南风3-4级转小于3级"),
                    new XElement("info", "较凉爽,建议着长袖衬衫加单裤等春秋过渡装。年老体弱者宜着针织长袖衬衫、马甲和长裤。"));

                XDocument xdoc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XComment("天气数据"), new XElement("root", xe));

                StreamWriter sw = new StreamWriter(isfs);
                xdoc.Save(sw);
                sw.Close();
            }
        }

好了,再试试吧

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值