Windows Phone 實用開發技巧(25):Windows Phone讀取本機資料

來源:互聯網
上載者:User

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

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 

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.