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 



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




相关文章
|
计算机视觉 Windows Python
windows下使用python + opencv读取含有中文路径的图片 和 把图片数据保存到含有中文的路径下
在Windows系统中,直接使用`cv2.imread()`和`cv2.imwrite()`处理含中文路径的图像文件时会遇到问题。读取时会返回空数据,保存时则无法正确保存至目标目录。为解决这些问题,可以使用`cv2.imdecode()`结合`np.fromfile()`来读取图像,并使用`cv2.imencode()`结合`tofile()`方法来保存图像至含中文的路径。这种方法有效避免了路径编码问题,确保图像处理流程顺畅进行。
2677 1
|
数据库 数据安全/隐私保护 Windows
Windows远程桌面出现CredSSP加密数据修正问题解决方案
【10月更文挑战第30天】本文介绍了两种解决Windows系统凭据分配问题的方法。方案一是通过组策略编辑器(gpedit.msc)启用“加密数据库修正”并将其保护级别设为“易受攻击”。方案二是通过注册表编辑器(regedit)在指定路径下创建或修改名为“AllowEncryptionOracle”的DWORD值,并将其数值设为2。
12958 3
|
缓存 NoSQL Linux
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
562 1
【Azure Redis 缓存】Windows和Linux系统本地安装Redis, 加载dump.rdb中数据以及通过AOF日志文件追加数据
|
Web App开发 存储 安全
微软警告数百万Windows用户:切勿冒险丢失所有数据
微软警告数百万Windows用户:切勿冒险丢失所有数据
微软警告数百万Windows用户:切勿冒险丢失所有数据
|
Ubuntu Linux Python
如何利用wsl-Ubuntu里conda用来给Windows的PyCharm开发
如何在WSL(Windows Subsystem for Linux)的Ubuntu环境中使用conda虚拟环境来为Windows上的PyCharm开发设置Python解释器。
3004 1
|
Linux Apache C++
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
该文介绍了如何在Windows环境下为FFmpeg集成SRT协议支持库libsrt。首先,需要安装Perl和Nasm,然后编译OpenSSL。接着,下载libsrt源码并使用CMake配置,生成VS工程并编译生成srt.dll和srt.lib。最后,将编译出的库文件和头文件按照特定目录结构放置,并更新环境变量,重新配置启用libsrt的FFmpeg并进行编译安装。该过程有助于优化直播推流的性能,减少卡顿问题。
791 2
FFmpeg开发笔记(三十五)Windows环境给FFmpeg集成libsrt
|
存储 安全 程序员
Windows任务管理器开发原理与实现
Windows任务管理器开发原理与实现
|
存储 缓存 安全
硬盘数据恢复:恢复硬盘数据的9个实用方法(Windows版)
无论是工作文档、家庭照片,还是其他珍贵的数字资产,数据丢失总是一件让人头疼的事情。然而,当硬盘发生问题时,不必过于慌张——只要正确应对,许多数据都可以被成功恢复。本文将从常见数据丢失原因到具体恢复方法,为您提供全面的硬盘数据恢复指导。
|
监控 关系型数据库 MySQL
PowerShell 脚本编写 :自动化Windows 开发工作流程
PowerShell 脚本编写 :自动化Windows 开发工作流程
911 0
|
开发者 C# Windows
WPF与游戏开发:当桌面应用遇见游戏梦想——利用Windows Presentation Foundation打造属于你的2D游戏世界,从环境搭建到代码实践全面解析新兴开发路径
【8月更文挑战第31天】随着游戏开发技术的进步,WPF作为.NET Framework的一部分,凭借其图形渲染能力和灵活的UI设计,成为桌面游戏开发的新选择。本文通过技术综述和示例代码,介绍如何利用WPF进行游戏开发。首先确保安装最新版Visual Studio并创建WPF项目。接着,通过XAML设计游戏界面,并在C#中实现游戏逻辑,如玩家控制和障碍物碰撞检测。示例展示了创建基本2D游戏的过程,包括角色移动和碰撞处理。通过本文,WPF开发者可更好地理解并应用游戏开发技术,创造吸引人的桌面游戏。
1163 0

热门文章

最新文章