日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 运维知识 > windows >内容正文

windows

Windows Phone xml数据的解析与绑定

發布時間:2024/4/15 windows 26 豆豆
生活随笔 收集整理的這篇文章主要介紹了 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数据的解析与绑定的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。