Windows Phone 实用开发技巧(25):Windows Phone读取本地数据
2011-10-15 09:19
453 查看
Windows Phone read local data
During windows phone development, sometimes we might be want to read some initial data from local resources. Data can be stored in XML, JSON, TXT or other formats. Unlike data files stored in Isolatedstorage, we cannot make changes to them or delete them (since they are built in Content or Resource). Which format is better ? Well, it depends. So let me make a simple demo to show you how we read initial data in windows phone.
Read XML Files
Assuming that we have following xml file
The root Element is Students which has four child element Student. How can we load them in windows phone .We can do that in many ways. Before we do that we create Student class first.
Way 1. Add System.Xml.Linq reference
then we can use following code to load students in one collection
Remeber to set Students.xml build action to Content.
Way 2. use Application.GetResourceStream
Note: that Application.GetResourceStream is suitable for all file format.
We can use Application.GetResourceStream to load txt files, json files , dat files and whatever format. What you need to do is prepare data and read the file stream, convert to what you want.
you can find source here
During windows phone development, sometimes we might be want to read some initial data from local resources. Data can be stored in XML, JSON, TXT or other formats. Unlike data files stored in Isolatedstorage, we cannot make changes to them or delete them (since they are built in Content or Resource). Which format is better ? Well, it depends. So let me make a simple demo to show you how we read initial data in windows phone.
Read XML Files
Assuming that we have following xml file
<?xml version="1.0" encoding="utf-8" ?> <Students> <Student> <Name>Alexis</Name> <Age>12</Age> <No>007</No> </Student> <Student> <Name>Tomcat</Name> <Age>20</Age> <No>62</No> </Student> <Student> <Name>Jacky</Name> <Age>32</Age> <No>001</No> </Student> <Student> <Name>Selina</Name> <Age>24</Age> <No>033</No> </Student> </Students>
The root Element is Students which has four child element Student. How can we load them in windows phone .We can do that in many ways. Before we do that we create Student class first.
public class Student { public string Name { get; set; } public int Age { get; set; } public string No { get; set; } }
Way 1. Add System.Xml.Linq reference
then we can use following code to load students in one collection
XElement root = XElement.Load("Students.xml"); if (root != null) { var items = from student in root.Descendants("Student") select new Student { Age = Convert.ToInt32(student.Element("Age").Value), Name = student.Element("Name").Value, No = student.Element("No").Value, }; listBox1.ItemsSource = items; }
Remeber to set Students.xml build action to Content.
Way 2. use Application.GetResourceStream
var streamInfo = Application.GetResourceStream(new Uri("Students.xml", UriKind.Relative)); using (var stream=streamInfo.Stream) { XElement root = XElement.Load(stream); if (root != null) { var items = from student in root.Descendants("Student") select new Student { Age = Convert.ToInt32(student.Element("Age").Value), Name = student.Element("Name").Value, No = student.Element("No").Value, }; listBox1.ItemsSource = items; } }
Note: that Application.GetResourceStream is suitable for all file format.
We can use Application.GetResourceStream to load txt files, json files , dat files and whatever format. What you need to do is prepare data and read the file stream, convert to what you want.
you can find source here
相关文章推荐
- Windows Phone 实用开发技巧(25):Windows Phone读取本地数据
- Windows Phone 实用开发技巧(1):图片缓存
- Windows Phone 实用开发技巧(23):强制关闭SIP
- Windows Phone实用开发技巧(32):照片角度处理
- Windows Phone 实用开发技巧(10):Windows Phone 中处理图片的技巧
- Windows Phone 实用开发技巧(9):自定义Windows Phone 页面切换动画
- Windows Phone 实用开发技巧(22):使用日志记录当前信息与异常信息
- Windows Phone 实用开发技巧(14):输入框隐藏SIP事件
- Windows Phone 实用开发技巧(6):windows phone 下汉字转拼音
- Windows Phone 实用开发技巧(29):动态绑定Pivot
- Windows Phone 实用开发技巧(26):对DataTemplate中的元素播放动画
- Windows Phone 实用开发技巧(10):Windows Phone 中处理图片的技巧 推荐
- Windows Phone实用开发技巧(38):图片拼接
- Windows Phone读取本地数据
- Windows Phone 实用开发技巧(6):windows phone 下汉字转拼音
- Windows Phone 实用开发技巧(30):Pivot切换时同时渐变背景图片
- Android开发:Sqlite 3 本地保存数据是否可以在不同的app应用下相互读取
- Windows Phone 7实用开发技巧(32):照片角度处理
- Windows Phone 实用开发技巧(27):创建透明Tile
- Windows Phone 实用开发技巧(9):自定义Windows Phone 页面切换动画