WP7编程 读取外部XML文件中存放的记录
Kagula
2011/8/9
内容简介
我准备写个简单的WP7程序,需要读取我在项目外部设置的XML文件中的记录集,作为Silverlight初学者,途中碰到了很多问题,现在把它记录下来,备查。
测试环境:[1]Visual2010+SP1
[2]Windows Phone SDK
正文
第一步:确定XML文件的格式
S1-1:先定义单条记录结构对应的C#代码
示例源码如下:
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; } } }
上面的记录有firstname、lastname、age三个字段组成。
S1-2:确定多条记录对应的C#代码
因为有多条记录组成,所以要使用下面的示例代码,确定多条记录情况下XML文件内容的格式,下面是示例代码,我们这里随便插入了三行记录
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; }
S1-3:现在记录集对应的的C#代码有了,下面的代码生成该记录集对应的示例XML文件内容
using System.Xml;
using System.Xml.Serialization;
using System.IO.IsolatedStorage;
using System.IO;
//这里省略若干行代码
private void writeToXML() { XmlWriterSettings xmlWriterSettings = new XmlWriterSettings(); xmlWriterSettings.Indent = true; StreamWriter stream = new StreamWriter("People.xml"); XmlSerializer serializer = new XmlSerializer(typeof(List<Person>)); using (XmlWriter xmlWriter = XmlWriter.Create(stream, xmlWriterSettings)) { serializer.Serialize(xmlWriter, GeneratePersonData()); } }把上面几段代码插入到新建的WindowsForm项目中并运行会生成People.xml文件,查看这个文件有我们需要的XML文件格式。我们可以修改这个XML文件插入我们实际的记录集内容。
第二步:WP7项目中引入这个XML文件,并读取其内容
S2-1:在WP7项目的References结点中添加”System.Xml.Serialization”库
S2-2:在WP7项目中新建文件夹,起名为“MyResource”在该文件夹下新建MyPeople.xml,用我们刚才生成的People.XML文件的内容,替换MyPeople.xml中的内容。
MyPeople.xml的Copy to Output Directory属性得设成Copy if newer。
参考下图
S2-3:WP7项目中使用下面的代码读取MyPeople.xml中的内容
try { Uri uri = new Uri("MyResource/MyPeople.xml", UriKind.Relative); StreamResourceInfo sri = Application.GetResourceStream(uri); if (sri!=null) { XmlSerializer serializer = new XmlSerializer(typeof(List<Person>)); List<Person> data = (List<Person>)serializer.Deserialize(sri.Stream); this.listBox.ItemsSource = data; } } catch { //add some code here }
上面代码中的“listBox”控件由下面XAML的代码段定义
总结
通过上面的代码我们实现了从我们手工定义内容的XML文件中读取记录集。这里要注意的是[1]在WP7中使用的Stream Class同在WinForm项目中使用的Stream Class类型的不同。[2]借助第一步生成XML文件结构,然后在这个结构上我们把自己记录的内容替换上去,这样解析XML文件的时候,才不会出错。
下面附WP7项目中MainPage.xaml.cs的源码
using System; using System.Collections.Generic; using System.Linq; using System.Net; using System.Windows; using System.Windows.Controls; using System.Windows.Documents; using System.Windows.Input; using System.Windows.Media; using System.Windows.Media.Animation; using System.Windows.Shapes; using Microsoft.Phone.Controls; using System.Xml; using System.Xml.Serialization; using System.IO.IsolatedStorage; using System.IO; using System.Windows.Resources; namespace PhoneApp3 { 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; } } } public partial class MainPage : PhoneApplicationPage { /* 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; } private void writeToXML() { 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()); } } } } private void readFromXML() { 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 } } */ // Constructor public MainPage() { InitializeComponent(); //writeToXML(); //readFromXML(); try { Uri uri = new Uri("MyResource/MyPeople.xml", UriKind.Relative); StreamResourceInfo sri = Application.GetResourceStream(uri); if (sri!=null) { XmlSerializer serializer = new XmlSerializer(typeof(List<Person>)); List<Person> data = (List<Person>)serializer.Deserialize(sri.Stream); this.listBox.ItemsSource = data; } } catch { //add some code here } } } }参考资料
[1]《WP7 Isolated Storage详解(5)-通过XmlSerializer读写XML文件》
http://www.cnblogs.com/zdave/archive/2011/05/23/2054344.html