Windows Phone 实用开发技巧(25):Windows Phone读取本地数据

本文介绍在Windows Phone开发中如何从本地资源读取初始数据,重点演示了两种读取XML文件的方法,并提供了示例代码。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

 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

<?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

image_thumb

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 



    本文转自xshf12345 51CTO博客,原文链接:http://blog.51cto.com/alexis/688264,如需转载请自行联系原作者




评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值