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


    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 

    如果您喜欢我的文章,您可以通过支付宝对我进行捐助,您的支持是我最大的动力https://me.alipay.com/alexis


    作者:Alexis
    出处:http://www.cnblogs.com/alexis/
    关于作者:专注于Windows Phone 7、Silverlight、Web前端(jQuery)。
    本文版权归作者和博客园共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,如有问题,可以通过shuifengxuATgmail.com  联系我,非常感谢。

  • 相关阅读:
    程序员达到高效率的一种境界 狼人:
    IBM夏然:软件产业对整个国家发展举足轻重 狼人:
    浅析Node.js:一个“编码就绪”服务器 狼人:
    eBay的Turmeric和VJet的源程序移到了GitHub上 狼人:
    程序员最常见的技术性误区 狼人:
    我不是一个工程师——我是一个软件开发者 狼人:
    【简讯】Adobe停止发布AIR for Linux版本 狼人:
    【赏析】15个非常棒的使用CSS3的设计组合 狼人:
    5款最好的免费Linux缓存系统 狼人:
    漫画:天堂里没有程序员! 狼人:
  • 原文地址:https://www.cnblogs.com/alexis/p/2212807.html
Copyright © 2020-2023  润新知