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

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文件结构:

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; }
    }
}

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

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文件并写入数据。

// 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上。

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。

<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方法,清理非托管资源。

作者: Zdave 发表于 2011-05-23 15:10 原文链接

推荐.NET配套的通用数据层ORM框架:CYQ.Data 通用数据层框架