Windows phone 之Xml操作
最近在做天气预报应用,这个应用中需要用到Xml来存储关注的城市列表,保存一下代码,方便以后使用,也给博友们一个参考:
其中:添加关注城市的操作代码是:
其实就是,
(1)先把数据从CareCityCode.xml文档中查询出来,放在集合A中,然后删除该文档。
(2)把新关注的城市添加到集合A中,
(3)把集合A中的数据再新建一个CareCityCode.xml,保存到该xml文档中。
#region 获取关注城市ID列表 --GetCareCityList /// <summary> /// 获取关注城市ID列表 /// </summary> /// <returns></returns> public static ObservableCollection<City> GetCareCityList() { ObservableCollection<City> list = new ObservableCollection<City>(); using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { if (isf.FileExists("CareCityCode.xml")) { using (IsolatedStorageFileStream isoStream = isf.OpenFile("CareCityCode.xml", FileMode.Open, FileAccess.Read)) { XElement xel = XElement.Load(isoStream); //MessageBox.Show("Xml文档的数据"+xel.ToString()); var citys = from city in xel.Descendants("CityCode") //orderby "DateTime" descending select city; foreach (var city in citys) { list.Add(new City { CityID = city.Element("ID").Value , CityName=city.Element("Name").Value}); } } } } return list; } #endregion
#region 添加关注城市ID --AddCareCity /// <summary> /// 添加关注城市ID /// </summary> /// <param name="CityID"></param> public static void AddCareCity(string CityID,string CityName) { using (IsolatedStorageFile isf = IsolatedStorageFile.GetUserStoreForApplication()) { if (isf.FileExists("CareCityCode.xml")) { ObservableCollection<City> list = GetCareCityList(); //判断添加的要关注的城市是否已经存在 foreach (var City in list) { if (City.CityID == CityID) { MessageBox.Show("已在关注列表中!"); return; } } list.Add(new City() { CityID=CityID, CityName=CityName}); isf.DeleteFile("CareCityCode.xml"); using (IsolatedStorageFileStream isoStream = isf.OpenFile("CareCityCode.xml", FileMode.OpenOrCreate, FileAccess.Write)) { XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), new XElement("CityCodes")); foreach (City temp in list) { doc.Element("CityCodes").Add(new XElement("CityCode", new XElement("ID", temp.CityID), new XElement("Name",temp.CityName))); } MessageBox.Show(doc.ToString()); doc.Save(isoStream); } } else { using (IsolatedStorageFileStream isoStream = isf.OpenFile("CareCityCode.xml", FileMode.OpenOrCreate, FileAccess.Write)) { XElement xel = new XElement("CityCodes", new XElement("CityCode", new XElement("ID", CityID), new XElement("Name",CityName)) ); XDocument doc = new XDocument(new XDeclaration("1.0", "utf-8", "yes"), xel); doc.Save(isoStream); MessageBox.Show(doc.ToString()); } } } } #endregion
删除代码:明天再贴,
而且我感觉这种方法很麻烦的,,,稍后会介绍一种比较简单的方法。
Windows phone 之Xml操作的更多相关文章
- 我也来学着写写WINDOWS服务-解析xml抓取数据并插入数据库
项目告一段落,快到一年时间开发了两个系统,一个客户已经在试用,一个进入客户测试阶段,中间突然被项目经理(更喜欢叫他W工)分派一个每隔两小时用windows服务去抓取客户提供的外网xml,解析该xml, ...
- C#:XML操作类
写的一个XML操作类,包括读取/插入/修改/删除. using System; using System.Data; using System.Configuration; using System. ...
- LINQ系列:LINQ to XML操作
LINQ to XML操作XML文件的方法,如创建XML文件.添加新的元素到XML文件中.修改XML文件中的元素.删除XML文件中的元素等. 1. 创建XML文件 string xmlFilePath ...
- T-Sql(五)xml操作
t-sql中的xml操作在我们平时做项目的过程中用的很少,因为我们处理的数据量很少,除非一些用到xml的地方,t-sql中xml操作一般用在数据量很大,性能优化的地方,当然我在平时做项目的时候也是没用 ...
- XML格式示例 与 XML操作(读取)类封装
header('Content-Type: text/xml'); <?xml version="1.0" encoding="utf-8" standa ...
- 【Java EE 学习 33 上】【JQuery样式操作】【JQuery中的Ajax操作】【JQuery中的XML操作】
一.JQuery中样式的操作 1.给id=mover的div采用属性增加样式.one $("#b1").click(function(){ $("#mover" ...
- 简单的XML操作类
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
- .net学习笔记---xml操作及读写
一.XML文件操作中与.Net中对应的类 微软的.NET框架在System.xml命名空间提供了一系列的类用于Dom的实现. 以下给出XML文档的组成部分对应.NET中的类: XML文档组成部分 对应 ...
- C#常用操作类库三(XML操作类)
/// <summary> /// XmlHelper 的摘要说明. /// xml操作类 /// </summary> public class XmlHelper { pr ...
随机推荐
- UVA 10256 The Great Divide (凸包,多边形的位置关系)
题目链接:http://acm.hust.edu.cn/vjudge/problem/viewProblem.action?id=34148 [思路] 凸包 求出红蓝点的凸包,剩下的问题就是判断两个凸 ...
- parseSdkContent failed Could not initialize class android.graphics.Typeface
Deleting ".android" is temporarily fixing the problem with me as after sometime it begins ...
- wxWidgets学习笔记——在屏幕上画简单的图形和文字
在屏幕上画简单图形和显示图片.处理简单鼠标键盘事件 /*************************************************************** * Name: M ...
- 模仿GsonConverter 写的StringConverter 解析String字符串
使用自己写的StringConverter 来封装的 Converter 来解析String private static final RestAdapter CAMERA_CLIENT_NETWOR ...
- js读取本地磁盘文本文件并保存为JSON数据(有格式的文本)
主要的代码是红色区域,HTML5获取本地文件对象并进行操作 //给上传按钮添加点击事件 $(".myappTXTUploadBtn").click(function(){ var ...
- Windows XP下安装WinCE6.0开发环境
Windows下怎样编译WinCE6.0及开发应用程序.以下介绍(安装之前必须保证C盘有足够的空间!20g左右!主要是由于在安装程序在安装过程中要解压): 在Visual Studio 2005之前, ...
- PureMVC(JS版)源码解析(七):Mediator类
之前的博文中,我们分析了SimpleCommand类和MacroCommand类,这两个类用作"业务逻辑(business logic)"处理,今天,我们讲一些和UI界面相关联的M ...
- 不一样的风格,C#的lambda表达式
下面贴出代码 using System; using System.Collections.Generic; using System.Linq; using System.Text; using S ...
- [转] Putty - 文件夹显示的蓝色太暗
Putty SSH key的后缀为ppk 默认文件夹的颜色显示为ANSI Blue,颜色太暗. ANSI Blue : RGB(0, 0, 187) 将ANSI Blue修改为和ANSI Blue B ...
- EF 命令
我们选VS中工具-库程序包管理器-程序包管理器控制台, 这时在VS底部会出现控制台 这时候我们要执行四个操作: 下载安装EF 到工程.PM> Install-Package EntityFram ...