(3)[wp7数据存储] WP7 IsolatedStorage系列篇——通过XmlSerializer读写XML文件 [复制链接]

发表于 2012-5-17 15:51:07 |只看该作者 |倒序浏览

 

本帖最后由 agameboy 于 2012-5-17 17:08 编辑

这一篇我们会通过XmlSerializer读写XML文件,跟多的相关文章请参考WP7 IsolatedStorage系列篇
需要的命名空间:
using System.IO;
using System.IO.IsolatedStorage;
using System.Xml;
using System.Xml.Serialization;

注意你需要在项目中添加System.Xml.Serialization引用,不然那你就无法实现喽!!

写一个Person类:

  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.     }
复制代码


下面为一个序列化方法:

  1. private List<Person> GeneratePersonData()
  2.         {
  3.             List<Person> data = new List<Person>();
  4.             data.Add(new Person() { FirstName ="Kate",LastName ="Brown",Age=23});
  5.             data.Add(new Person() { FirstName = "Kitty", LastName = "Brown", Age = 22 });
  6.             data.Add(new Person() { FirstName = "Mic", LastName = "Brown", Age = 21 });
  7.             return data;
  8.         }
复制代码


保存XML文件:

  1. private void button1_Click(object sender, RoutedEventArgs e)
  2.         {
  3.             XmlWriterSettings xmlwriterSettings = new XmlWriterSettings();
  4.             xmlwriterSettings.Indent = true;//缩进
  5.             using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
  6.             {
  7.                 using(IsolatedStorageFileStream isoStream=iso.OpenFile("People.xml",FileMode.Create))
  8.                 {
  9.                     XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
  10.                     using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, xmlwriterSettings))
  11.                     {
  12.                         serializer.Serialize(xmlWriter, GeneratePersonData());
  13.                     }
  14.                 }
  15.             }
  16.         }
复制代码


读取XML文件:

  1. private void button2_Click(object sender, RoutedEventArgs e)
  2.         {
  3.            using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
  4.            {
  5.                 using(IsolatedStorageFileStream isoStream=iso.OpenFile("People.xml",FileMode.Open))
  6.                 {
  7.                     XmlSerializer serializer=new XmlSerializer (typeof(List<Person>));
  8.                     List<Person> data = (List<Person>)serializer.Deserialize(isoStream);
  9.                     listBox1.ItemsSource = data;
  10.                 }
  11.            }
  12.         }
复制代码


Binding ListBox:
  1. <ListBox Height="532" HorizontalAlignment="Left" Margin="0,69,0,0" Name="listBox1" VerticalAlignment="Top" Width="450" >
  2.                 <ListBox.ItemTemplate>
  3.                     <DataTemplate >
  4.                         <StackPanel Margin="10" >
  5.                             <TextBlock Height="30"  Name="textBlock1" Text="{Binding FirstName}" />
  6.                             <TextBlock Height="30"  Name="textBlock2" Text="{Binding LastName}"  />
  7.                             <TextBlock Height="30"  Name="textBlock3" Text="{Binding Age}"/>
  8.                         </StackPanel>
  9.                     </DataTemplate>
  10.                 </ListBox.ItemTemplate>
  11.             </ListBox>
复制代码


提示:当进行文件操作的时候始终使用using关键字,using结束后会隐式调用Disposable方法,清理非托管资源。
posted @ 2012-11-29 14:33  BellingWP  阅读(129)  评论(0编辑  收藏  举报