通过XmlSerializer读写XML文件

首先创建一个Windows Phone 7项目,然后在MainPage.xaml.cs(或其他页面文件)中引入命名空间:

?
1
2
3
4
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;

提示:你需要在项目中添加System.Xml.Serialization引用。

对于很多应用,向隔离存储空间读写XML文件是很常见的任务。

一般情况下我们使用类IsolatedStorageFileStream进行读、写、创建文件等操作。与使用XmlWriter不同的是,XmlSerializer使我们更方便的在Object和XML文档之间进行序列化与反序列化转换。

本篇中我们将使用如下的Person类来生成XML文件结构:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
public class Person
{
string firstname;
string lastname;
int age;
public string FirstName
{
get { return firstname; }
set { firstname = value; }
}
public string LastName
{
get { return lastname; }
set { lastname = value; }
}
public int Age
{
get { return age; }
set { age = value; }
}
}

将要被序列化的数据对象:

?
1
2
3
4
5
6
7
8
private List<Person> GeneratePersonData()
{
List<Person> data = new List<Person>();
data.Add( new Person() { FirstName = "Kate" , LastName = "Brown" , Age = 25 });
data.Add( new Person() { FirstName = "Tom" , LastName = "Stone" , Age = 63 });
data.Add( new Person() { FirstName = "Michael" , LastName = "Liberty" , Age = 37 });
return data;
}
使用XmlSerializer 保存XML文件到隔离存储空间

示例中创建了一个名为People.xml的XML文件并写入数据。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
// Write to the Isolated Storage
XmlWriterSettings xmlWriterSettings = new XmlWriterSettings();
xmlWriterSettings.Indent = true ;
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile( "People.xml" , FileMode.Create))
{
XmlSerializer serializer = new XmlSerializer( typeof (List<Person>));
using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings))
{
serializer.Serialize(xmlWriter, GeneratePersonData());
}
}
}

提示:创建文件使用FileMode.Create,写入内容使用FileAccess.WriteFileAccess的成员共有Read、Write、ReadWrite三种。

使用XmlSerializer从隔离存储空间中读取XML文件

示例中打开了一个已存在的文件People.xml并读取它的内容。然后把数据显示在一个ListBox上。

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
try
{
using (IsolatedStorageFile myIsolatedStorage = IsolatedStorageFile.GetUserStoreForApplication())
{
using (IsolatedStorageFileStream stream = myIsolatedStorage.OpenFile( "People.xml" , FileMode.Open))
{
XmlSerializer serializer = new XmlSerializer( typeof (List<Person>));
List<Person> data = (List<Person>)serializer.Deserialize(stream);
this .listBox.ItemsSource = data;
}
}
}
catch
{
//add some code here
}

提示:打开已存在文件使用FileMode.Open,读取内容使用FileAccess.Read。

?
1
2
3
4
5
6
7
8
9
10
11
< ListBox x:Name = "listBox" >
< ListBox.ItemTemplate >
< DataTemplate >
< StackPanel Margin = "10" >
< TextBlock Text = "{Binding FirstName}" />
< TextBlock Text = "{Binding LastName}" />
< TextBlock Text = "{Binding Age}" />
</ StackPanel >
</ DataTemplate >
</ ListBox.ItemTemplate >
</ ListBox >

提示:当进行文件操作的时候始终使用using关键字,using结束后会隐式调用Disposable方法,清理非托管资源。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值