WP7的XML操作详解(转)

在这个小教程,我将演示在Windows Phone 7如何让ListBox的数据绑定XML数据。我将使用LINQ to XML,以便加载和读取数据,而且我将展示如何实现一个基本的过滤。 首先让我们先创建一个Windows Phone 7的应用程序项目示例,并添加以下两个demo xml文件。 people.xml
  1. <?xml version="1.0" encoding="utf-8" ?>
  2. <people>
  3. <person>
  4. <firstname>Kate</firstname>
  5. <lastname>Smith</lastname>
  6. <age>27</age>
  7. </person>
  8. <person>
  9. <firstname>Tom</firstname>
  10. <lastname>Brown</lastname>
  11. <age>30</age>
  12. </person>
  13. <person>
  14. <firstname>Tim</firstname>
  15. <lastname>Stone</lastname>
  16. <age>36</age>
  17. </person>
  18. <person>
  19. <firstname>Ann</firstname>
  20. <lastname>Peterson</lastname>
  21. <age>27</age>
  22. </person>
  23. </people>
复制代码
PeopleCustom.xml
  1. <?xml version="1.0" ?>
  2. <People>
  3. <Person
  4. FirstName="Kate"
  5. LastName="Smith"
  6. Age="27" />
  7. <Person
  8. FirstName="Tom"
  9. LastName="Brown"
  10. Age="30" />
  11. <Person
  12. FirstName="Tim"
  13. LastName="Stone"
  14. Age="36" />
  15. <Person
  16. FirstName="Ann"
  17. LastName="Peterson"
  18. Age="27" />
  19. </People>
复制代码
下一步是创建一个示例类将被用来存储XML元素值:
  1. public class Person
  2. {
  3. string firstname;
  4. string lastname;
  5. int age;
  6. public string FirstName
  7. {
  8. get { return firstname; }
  9. set { firstname = value; }
  10. }
  11. public string LastName
  12. {
  13. get { return lastname; }
  14. set { lastname = value; }
  15. }
  16. public int Age
  17. {
  18. get { return age; }
  19. set { age = value; }
  20. }
  21. }
复制代码
您所在的用户组无法下载或查看附件
为了读取XML文件的信息,我们将使用的XDocument 所以你首先需要添加System.Xml.Linq.dll引用,然后using System.Xml.Linq;
  1. XDocument loadedData = XDocument.Load("People.xml");
  2. var data = from query in loadedData.Descendants("person")
  3.               select new Person
  4.               {
  5.                   FirstName = (string)query.Element("firstname"),
  6.                   LastName = (string)query.Element("lastname"),
  7.                   Age = (int)query.Element("age")
  8.               };
  9. listBox.ItemsSource = data;
复制代码
在接下来的例子中,我们将通过数据的“年龄”属性值过滤。源代码如下:
  1. XDocument loadedCustomData = XDocument.Load("PeopleCustom.xml");
  2. var filteredData = from c in loadedCustomData.Descendants("Person")
  3.             where c.Attribute("Age").Value == "27"
  4.             select new Person()
  5.             {
  6.                 FirstName = c.Attribute("FirstName").Value,
  7.                 LastName = c.Attribute("LastName").Value
  8.                 
  9.             };
  10. listBox1.ItemsSource = filteredData;
复制代码
为了显示的数据,我们将使用以下ItemTemplates绑定ListBox控件:
  1. <StackPanel x:Name="ContentPanel" Grid.Row="1" Margin="12,0,12,0" Orientation="Horizontal">
  2.     <TextBlock Text="XML Data:"/>
  3.     <ListBox x:Name="listBox">
  4.         <ListBox.ItemTemplate>
  5.             <DataTemplate>
  6.                 <StackPanel Margin="10" >
  7.                     <TextBlock Text="{Binding FirstName}"/>
  8.                     <TextBlock Text="{Binding LastName}"/>
  9.                     <TextBlock Text="{Binding Age}"/>
  10.                 </StackPanel>
  11.             </DataTemplate>
  12.         </ListBox.ItemTemplate>
  13.     </ListBox>
  14.     <TextBlock Text="Filtered by Age 27:"/>
  15.     <ListBox x:Name="listBox1">
  16.         <ListBox.ItemTemplate>
  17.             <DataTemplate>
  18.                 <StackPanel Margin="20" >
  19.                     <TextBlock Text="{Binding FirstName}"/>
  20.                     <TextBlock Text="{Binding LastName}"/>
  21.                 </StackPanel>
  22.             </DataTemplate>
  23.         </ListBox.ItemTemplate>
  24.     </ListBox>
  25. </StackPanel>
复制代码

转载于:https://www.cnblogs.com/quan448600140/archive/2012/06/26/2563123.html

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值