Students.xml<<FONT face="Courier New">?xml version="1.0" encoding="utf-8" ?>
<<SPAN style="COLOR: #808000">Student><<SPAN style="COLOR: #808000">user><<SPAN style="COLOR: #808000">headurl>http://tp3.sinaimg.cn/1948192014/50/5601371558/1<<SPAN style="COLOR: #808000">name>张小三<<SPAN style="COLOR: #808000">/name><<SPAN style="COLOR: #808000">age>20<<SPAN style="COLOR: #808000">/age><<SPAN style="COLOR: #808000">/user><<SPAN style="COLOR: #808000">user><<SPAN style="COLOR: #808000">headurl>http://tp3.sinaimg.cn/1948192014/50/5601371558/1<<SPAN style="COLOR: #808000">name>李小四<<SPAN style="COLOR: #808000">/name><<SPAN style="COLOR: #808000">age>18<<SPAN style="COLOR: #808000">/age><<SPAN style="COLOR: #808000">/user><<SPAN style="COLOR: #808000">user><<SPAN style="COLOR: #808000">headurl>http://tp3.sinaimg.cn/1948192014/50/5601371558/1<<SPAN style="COLOR: #808000">name>王小五<<SPAN style="COLOR: #808000">/name><<SPAN style="COLOR: #808000">age>22<<SPAN style="COLOR: #808000">/age><<SPAN style="COLOR: #808000">/user><<SPAN style="COLOR: #808000">user><<SPAN style="COLOR: #808000">headurl>http://tp3.sinaimg.cn/1948192014/50/5601371558/1<<SPAN style="COLOR: #808000">name>马小六<<SPAN style="COLOR: #808000">/name><<SPAN style="COLOR: #808000">age>20<<SPAN style="COLOR: #808000">/age><<SPAN style="COLOR: #808000">/user>
<<SPAN style="COLOR: #808000">/Student

StudentInfo.xml<<FONT face="Courier New">?xml version="1.0" encoding="utf-8" ?>
<<SPAN style="COLOR: #808000">Student><</FONT>userheadurl="http://tp3.sinaimg.cn/1948192014/50/5601371558/1"name="张小三"age="20"/><</FONT>userheadurl="http://tp3.sinaimg.cn/1948192014/50/5601371558/1"name="李小四"age="18"/><</FONT>userheadurl="http://tp3.sinaimg.cn/1948192014/50/5601371558/1"name="王小五"age="22"/><</FONT>userheadurl="http://tp3.sinaimg.cn/1948192014/50/5601371558/1"name="马小六"age="20"/><</FONT>userheadurl="http://tp3.sinaimg.cn/1948192014/50/5601371558/1"name="贾小可"age="22"/><<SPAN style="COLOR: #808000">/Student>

下面创建一个Model类,来对数据进行存取。

public class Model
{  string headurl="";  string nameurl="";  int age;  public string HeadUrl { get; set; }  public string Name { get; set; }  public int Age { get; set; }
} 

这是前台代码,主要是控件的摆放和绑定操作:

<<FONT face="Courier New">Grid x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0"><<SPAN style="COLOR: #808000">ListBox HorizontalAlignment="Left" Margin="12,6,0,18"
Name="listBox1" Width="342" ><<SPAN style="COLOR: #808000">ListBox.ItemTemplate><<SPAN style="COLOR: #808000">DataTemplate><<SPAN style="COLOR: #808000">StackPanel Orientation="Horizontal"><<SPAN style="COLOR: #808000">Image Source="{Binding HeadUrl}"
Width="50" Height="50"/><<SPAN style="COLOR: #808000">StackPanel><<SPAN style="COLOR: #808000">TextBlock Text="{Binding Name}"/><<SPAN style="COLOR: #808000">TextBlock Text="{Binding Age}"/><<SPAN style="COLOR: #808000">/StackPanel><<SPAN style="COLOR: #808000">/StackPanel><<SPAN style="COLOR: #808000">/DataTemplate><<SPAN style="COLOR: #808000">/ListBox.ItemTemplate><<SPAN style="COLOR: #808000">/ListBox><<SPAN style="COLOR: #808000">ListBox Height="295" HorizontalAlignment="Left"
Margin="360,6,0,0" Name="listBox2" VerticalAlignment="Top" Width="338"><<SPAN style="COLOR: #808000">ListBox.ItemTemplate><<SPAN style="COLOR: #808000">DataTemplate><<SPAN style="COLOR: #808000">StackPanel Orientation="Horizontal"><<SPAN style="COLOR: #808000">Image Source="{Binding HeadUrl}"
Width="50" Height="50"/><<SPAN style="COLOR: #808000">StackPanel><<SPAN style="COLOR: #808000">TextBlock Text="{Binding Name}"/><<SPAN style="COLOR: #808000">TextBlock Text="{Binding Age}"/><<SPAN style="COLOR: #808000">/StackPanel><<SPAN style="COLOR: #808000">/StackPanel><<SPAN style="COLOR: #808000">/DataTemplate><<SPAN style="COLOR: #808000">/ListBox.ItemTemplate><<SPAN style="COLOR: #808000">/ListBox><<SPAN style="COLOR: #808000">/Grid>

为了读取XML文件中的信息,我们需要添加一个.Net库支持 System.Xml.Linq.我们会使用到里面的XDocument相关类的操作。

接下来我们进行查询及绑定,直接看代码吧:

XDocument xdoc = XDocument.Load("Students.xml");  var student = from query in xdoc.Descendants("user")
select new Model
{  HeadUrl = (string)query.Element("headurl"),  Name = (string)query.Element("name"),  Age=(int)query.Element("age")
};
this.listBox1.ItemsSource = student; 

效果如下图:

要进行数据的过滤操作,需要对各个元素的属性值进行判断,我们使用下面那个StudentsInfo文件进行判断,当然用Students也可以,这里就不多说了。继续看代码:

XDocument xdoc1 = XDocument.Load("StudentsInfo.xml");
var student1 = from query in xdoc1.Descendants("user")
where query.Attribute("age").Value == "20"
select new Model
{  HeadUrl = query.Attribute("headurl").Value,  Name = query.Attribute("name").Value,  Age =int.Parse(query.Attribute("age").Value)
};
this.listBox2.ItemsSource = student1; 

为了醒目我将上面两个例子放在一起显示,如图:

下面是一个按某个元素来进行排序的例子,我们就按年龄来对刚才的数据进行一个升序排列:

XDocument xdoc1 = XDocument.Load("StudentsInfo.xml");
var student1 = from query in xdoc1.Descendants("user")
orderby int.Parse(query.Attribute("age").Value) ascending  select new Model  {  HeadUrl = query.Attribute("headurl").Value,  Name = query.Attribute("name").Value,  Age =int.Parse(query.Attribute("age").Value)  };  this.listBox2.ItemsSource = student1; 

注意:按降序排列的话就把ascending改成descending可以了,默认是按升序排列的。

看图:

下面再介绍另一种查找方法,感觉也非常实用:

public ObservableCollection studentCollection { get; private set; }
XDocument xdoc2 = XDocument.Load("Students.xml");
studentCollection = new ObservableCollection();
foreach (XElement element in xdoc2.Element("Student").Descendants("user"))  {  studentCollection.Add(new Model()  {  HeadUrl = element.Element("headurl").Value,  Name = element.Element("name").Value,  Age =Int32.Parse(element.Element("age").Value)  });  }  this.listBox1.ItemsSource = studentCollection; 

效果如下:

要进行数据的筛选等操作只需要在foreach里面进行判断就可以了,也是很方便的。而且使用了ObservableCollection这个集合,操作起来也会十分方便。

先介绍这些吧,以后有空再进行深入介绍。

源码下载

本文来自石霖的博客,原文地址:http://www.cnblogs.com/ezra/archive/2011/06/28/2092033.html

Windows Phone xml数据的解析与绑定相关推荐

  1. php post 获取xml,php 获取post的xml数据并解析示例

    这篇文章主要为大家详细介绍了php 获取post的xml数据并解析示例,具有一定的参考价值,可以用来参考一下. 对php获取post过来的xml数据并解析感兴趣的小伙伴,下面一起跟随512笔记的小编两 ...

  2. python解析xml数据_Python解析XML数据方法

    Python在采取数据方面真实相当的简洁和方便,对于个人草根站长来说学一点就已经能很好的把自己想要的功能实现:让自己做一个有技术的SEOER,并不困难,可能很多人没学就开始问学Python要多久,零基 ...

  3. php http请求xml数据,php获取通过http协议post提交过来xml数据及解析xml

    $xml_data =''. ''. '1234567890'. 'lgsoftwares'. 'mypassword'. 'phpmind.com'. ''. ''. ''. ''. ''. ''. ...

  4. WebService传递XML数据 C#DataSet操作XML 解析WebService返回的XML数据

    贴图  知乎:显著提升程序员身心健康和工作效率的装备有哪些? 笔记本:  1.银河舰队 PAVILION 15-bc011TX光暗影精灵2  2.顽石 -FL5900U7500超薄i7(性价比最高) ...

  5. ajax请求json和xml数据及对json和xml格式数据的解析

    ajax请求json和xml数据及对json和xml格式数据的解析 一.ajax请求json数据并解析 ajax的写法: json数据解析: 请求json经常出现的跨域报错: 二.ajax请求xml数 ...

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

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

  7. C#解析json和xml数据

    C#解析json和xml数据 C#解析json和xml数据 // 用到的包using Newtonsoft.Json; // using Newtonsoft.Json.Linq; const str ...

  8. 爬虫学习4-HTML和XML数据的分析与解析

    目前在 Java 中,解析 HTML 工具主要包含以下几种: 1,jsoup:强大的 HTML 解析工具,支持以 jQuery 中 CSS Selector 的方式提取 HTML 中的元素,学习成本较 ...

  9. iOS - XML 数据解析

    前言 @interface NSXMLParser : NSObjectpublic class NSXMLParser : NSObject 1.XML 数据 XML(Extensible Mark ...

最新文章

  1. python怎么重新启动内核_通过“ipython kernel”重新启动ipython内核
  2. 【 C 】动态内存分配实用案例(二)之复制字符串
  3. 双11行业“三连冠”,鞋王百丽走对了哪几步?
  4. 查看使用的那个USB口和开发板通讯
  5. java--模板方法模式
  6. .NET Core 3.0之深入源码理解ObjectPool(二)
  7. linux和GNU之间的关系
  8. 事务嵌套问题_注意Spring事务这一点,避免出现大事务
  9. Navicat PatchNavicat
  10. 3s新闻周刊第9期,本期策划:电子地图的出路
  11. 微软9月补丁星期二值得关注的0day、终于落幕的 PrintNightmare及其它
  12. 数据存储技术-专题介绍
  13. 过拟合和欠拟合_TensorFlow教程-过拟合和欠拟合
  14. [No0000151]菜鸟理解.NET Framework中的CLI,CLS,CTS,CLR,FCL,BCL
  15. SmartImageView
  16. 使用ARKit编码测量应用程序:对象和阴影
  17. 了解一下PMO项目管理岗
  18. 【数理统计】02. 抽样分布与次序统计量
  19. Linux扩展ip上限,Linux之iptables添加扩展模块实现封P2P、封国家IP
  20. win7虚拟机iOS坑爹法语键盘冲突解决方法

热门文章

  1. eclipse 设置working directory
  2. Android -ui控件
  3. 企业绩效管理系统之平衡记分卡
  4. 分享web前端七款HTML5 Loading动画特效集锦
  5. c++ 或者 vc++中判断程序实例是否运行
  6. Nginx的启动、停止和重启
  7. Delphi中 StrToIntDef函数的用法
  8. Redis安装及基本配置
  9. ios动态获取UILabel的高度和宽度
  10. 自定义Dialog(图片,文字说明,单选按钮)----类ListPreference实现(2)