前述:这个知识是在Windows8.1或WP8.1中运用Linq to xml获取一个xml文件里的数据。(网上也很多类似的知识,可以借鉴参考)

平台:windows8.1 metro 或者WP8.1

步骤:1、在项目中准备一个xml文件。我在项目中建立了一个city.xml,如图:

city.xml具体代码如下:

  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <China>
  3. <city>
  4. <id>1</id>
  5. <name>北京</name>
  6. <description>中国的首都</description>
  7. </city>
  8. <city>
  9. <id>2</id>
  10. <name>深圳</name>
  11. <description>经济繁荣</description>
  12. </city>
  13. <city>
  14. <id>3</id>
  15. <name>广州</name>
  16. <description>很大的城市</description>
  17. </city>
  18. <city>
  19. <id>4</id>
  20. <name>香港</name>
  21. <description>亚洲金融中心</description>
  22. </city>
  23. <province>
  24. <id>1</id>
  25. <name>广东省</name>
  26. <city>
  27. <id>5</id>
  28. <name>佛山</name>
  29. <description>功夫之地</description>
  30. </city>
  31. <city>
  32. <id>6</id>
  33. <name>河源</name>
  34. <description>天是蓝的</description>
  35. </city>
  36. </province>
  37. </China>

2、创建一个对应city.xml的sampldata类

代码如下:

  1. public class City
  2. {
  3. private int id;
  4.  
  5. public int Id
  6. {
  7. get { return id; }
  8. set { id = value; }
  9. }
  10. private string name;
  11.  
  12. public string Name
  13. {
  14. get { return name; }
  15. set { name = value; }
  16. }
  17. private string description;
  18.  
  19. public string Description
  20. {
  21. get { return description; }
  22. set { description = value; }
  23. }
  24. }
  25. public class Province//这个是为了测试
  26. {
  27. private int id;
  28.  
  29. public int Id
  30. {
  31. get { return id; }
  32. set { id = value; }
  33. }
  34. private string name;
  35.  
  36. public string Name
  37. {
  38. get { return name; }
  39. set { name = value; }
  40. }
  41.  
  42. private List<City> cities;
  43.  
  44. public List<City> Cities
  45. {
  46. get { return cities; }
  47. set { cities = value; }
  48. }

3、UI布局(在WP8.1上,布局效果可以更好看点)

MainPage.xmal代码如下:

  1. <StackPanel Orientation="Horizontal">
  2. <Button Name="getxml" Content="获取数据1" Width="" Height="" VerticalAlignment="Top" Click="getxml_Click"/>
  3. <Button Name="getxml2" Content="获取数据2" Width="" Height="" VerticalAlignment="Top" Click="getxml2_Click" Margin="80,0,0,0"/>
  4. </StackPanel>
  5. <ListView x:Name="datalist" Margin="0, 50,0,0">
  6. <ListView.ItemTemplate>
  7. <DataTemplate>
  8. <StackPanel Orientation="Horizontal">
  9. <TextBlock Text="{Binding Id}" Style="{ThemeResource TitleTextBlockStyle}"/>
  10. <StackPanel Orientation="Vertical">
  11. <TextBlock Text="{Binding Name}" Style="{ThemeResource ListViewItemContentTextBlockStyle}"/>
  12. <TextBlock Text="{Binding Description}" Style="{ThemeResource BodyTextBlockStyle}"/>
  13. </StackPanel>
  14. </StackPanel>
  15. </DataTemplate>
  16. </ListView.ItemTemplate>
  17. </ListView>

4、后台具体代码

Note:这里需要 using System.Xml.Linq;

两个Button的点击事件代码如下:

  1. private async void getxml_Click(object sender, RoutedEventArgs e)
  2. {
  3. //在windows8.1中还可以获取xml文件,但在WP8.1会出错,我不知道为什么,求解释
  4. //string path = Path.Combine(Package.Current.InstalledLocation.Path, "DataModel/city.xml");//文件Uri
  5. //XDocument xmlfile = XDocument.Load(path);
  6.  
  7. //在Windows8.1和WP8.1中都可以执行,获取xml文件
  8. XDocument xmlfile;
  9. Uri fileUri = new Uri(@"ms-appx:///DataModel/city.xml");//ms-appx:// 为安装目录
  10. var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(fileUri);//获取city.xml文件, XDocument.Load()可利用stream,Uri,xmlreader等几种方法获取文件
  11. using (var stream = await file.OpenStreamForReadAsync())//文件操作都要转换成流
  12. {
  13. xmlfile = XDocument.Load(stream);
  14. };
  15.  
  16. var data = from query in xmlfile.Descendants("city")//获取city.xml中所有名为city节点
  17. select new City
  18. {
  19. Id = (int)query.Element("id"),
  20. Name = (string)query.Element("name"),
  21. Description = (string)query.Element("description")
  22. };
  23. datalist.ItemsSource = data;
  24. }
  25.  
  26. private async void getxml2_Click(object sender, RoutedEventArgs e)
  27. {
  28. datalist.ItemsSource = null;
  29. XDocument xmlfile;
  30. Uri fileUri = new Uri(@"ms-appx:///DataModel/city.xml");//ms-appx:// 为安装目录
  31. var file = await Windows.Storage.StorageFile.GetFileFromApplicationUriAsync(fileUri);//获取city.xml文件
  32. using (var stream = await file.OpenStreamForReadAsync())//文件操作都要转换成流
  33. {
  34. xmlfile = XDocument.Load(stream);
  35. };
  36. var data = from query in xmlfile.Descendants("city")
  37. where (string)query.Parent.Element("name") =="广东省"//linq to xml 选择id<3的city
  38. orderby (int)query.Element("id")
  39. select new City
  40. {
  41. Id = (int)query.Element("id"),
  42. Name = (string)query.Element("name"),
  43. Description = (string)query.Element("description")
  44. };
  45. datalist.ItemsSource = data;
  46. }

上面主要运用了Linq to xml, 需要注意的是Windows8.1和WP8.1在获取xml文件的方法有点差异,或者我哪里弄错了,请大家指出。还有请大家教下我怎样利用LINQ把province节点的数据绑定到Province类上,并给data赋值。

运行效果:

点击获取数据1按钮,效果如图:                  点击获取数据2按钮,效果如图:

                         

-----------------------------------------------------------------------------------------------------------个人总结

写完了这个知识积累了,自学这些编程真的有点学得慢,有个人带就好了啊。又要去复习集成电源和嵌入式了,为了明天下午的考试=.=还有明天上午要去财富世纪广场面试.Net实习生了,不知道凭借自己的现在的知识能不能通过,希望顺顺利利。

WinRT知识积累1之读xml数据的更多相关文章

  1. 机器学习等知识--- map/reduce, python 读json数据。。。

    map/ reduce 了解: 简单介绍map/reduce 模式: http://www.csdn.net/article/2013-01-07/2813477-confused-about-map ...

  2. WinRT知识积累2之MessageDialog应用代码

    private void NavigationHelper_SaveState(object sender, SaveStateEventArgs e) { // TODO: 在此处保存页面的唯一状态 ...

  3. Android网络之数据解析----SAX方式解析XML数据

    ​[声明] 欢迎转载,但请保留文章原始出处→_→ 生命壹号:http://www.cnblogs.com/smyhvae/ 文章来源:http://www.cnblogs.com/smyhvae/p/ ...

  4. Asp.net MVC知识积累

    一.知识积累 http://yuangang.cnblogs.com/ 跟蓝狐学mvc教程专题目录:http://www.lanhusoft.com/Article/169.html 依赖注入:htt ...

  5. J2EE 基础知识积累

    1. 面向对象的思维: 1. 有哪些类 那些对象      2. 这些类中,每种类应该具有某种属性和方法      3. 考虑类与类之间应该具有什么样的关系 3. 1. 成员变量可以使用java语言中 ...

  6. (四)SAX方式解析XML数据

    SAX方式解析XML数据 ​文章来源:http://www.cnblogs.com/smyhvae/p/4044170.html 一.XML和Json数据的引入: 通常情况下,每个需要访问网络的应用程 ...

  7. 网络相关系列之四:数据解析之SAX方式解析XML数据

    一.XML和Json数据的引入: 通常情况下.每一个须要訪问网络的应用程序都会有一个自己的server.我们能够向server提交数据,也能够从server获取数据.只是这个时候就有一个问题,这些数据 ...

  8. 数据库相关知识积累(sqlserver、oracle、mysql)

    数据库相关知识积累(sqlserver.oracle.mysql) 1. sqlserver :断开所有连接: (还原数据库) 1.数据库  分离 2. USE master GO ALTER DAT ...

  9. Ajax跨域访问XML数据的另一种方式——使用YQL查询语句

    XML数据默认是不能在客户端通过Ajax跨域请求读取的,一般的做法是在服务器上写一个简单的代理程序,将远程XML的数据先读到本地服务器,然后客户端再从本地服务器通过Ajax来请求.由于我们不能对数据源 ...

随机推荐

  1. maven参考文章推荐

    maven依赖.聚合.继承.版本管理:https://my.oschina.net/u/204498/blog/545724 maven profile : http://elim.iteye.com ...

  2. Notepad++的列编辑功能

    转自:http://www.crifan.com/files/doc/docbook/rec_soft_npp/release/htmls/index.html http://www.crifan.c ...

  3. android项目的结构和布局

    一.res文件夹 1.res文件夹用于存放Android的资源.包括:动画.静态图片.字符串.菜单.布局.视频.文件等. 1.drawable-ldpi:低分辨率图形(120像素/英寸) 2.draw ...

  4. Packets(模拟 POJ1017)

    Packets Time Limit: 1000MS Memory Limit: 10000K Total Submissions: 47750 Accepted: 16182 Description ...

  5. (转)函数调用方式与extern "C"

    原文:http://patmusing.blog.163.com/blog/static/13583496020103233446784/ (VC编译器下) 1. CALLBACK,WINAPI和AF ...

  6. elasticsearch之python备份

    一:elasticsearch原理 Elasticsearch是一个基于Apache Lucene(TM)的开源搜索引擎.无论在开源还是专有领域,Lucene可以被认为是迄今为止最先进.性能最好的.功 ...

  7. [问题2014S11] 复旦高等代数II(13级)每周一题(第十一教学周)

    [问题2014S11]  设 \(A,B\) 为 \(n\) 阶实对称阵, \(p(A),p(B),p(A+B)\) 分别为 \(A,B,A+B\) 的正惯性指数, 证明: \[p(A)+p(B)\g ...

  8. 又见JavaWeb的中文乱码

    简单翻了一下记录,我已经写了至少4篇关于编码和乱码的博客了,每次都觉得自己懂了. 实际上,这次的遭遇证明了"真懂"是一种很难达到的境界,吾辈仍需努力! 一.背景是这样子的: .一个 ...

  9. Deep Learning Papers Reading Roadmap

    Deep Learning Papers Reading Roadmap https://github.com/songrotek/Deep-Learning-Papers-Reading-Roadm ...

  10. C#中调用Matlab人工神经网络算法实现手写数字识别

    手写数字识别实现 设计技术参数:通过由数字构成的图像,自动实现几个不同数字的识别,设计识别方法,有较高的识别率 关键字:二值化  投影  矩阵  目标定位  Matlab 手写数字图像识别简介: 手写 ...