Windows Phone xml数据的解析与绑定
生活随笔
收集整理的這篇文章主要介紹了
Windows Phone xml数据的解析与绑定
小編覺得挺不錯的,現在分享給大家,幫大家做個參考.
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数据的解析与绑定的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: 查找域名、由名字查找某个熟知的端口、由名
- 下一篇: NO11 java5的线程锁技术(Loc