苏州实力做网站公司,建网站注意什么,免费建站并且绑定域名,淘宝网站750海报怎么做发表于 2012-5-17 15:51:07 |只看该作者 |倒序浏览 分享到#xff1a;本帖最后由 agameboy 于 2012-5-17 17:08 编辑这一篇我们会通过XmlSerializer读写XML文件#xff0c;跟多的相关文章请参考WP7 IsolatedStorage系列篇#xff01;需要的命名空间#xff1a;using System… 发表于 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类 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 ListPerson GeneratePersonData() { ListPerson data new ListPerson(); data.Add(new Person() { FirstName Kate,LastName Brown,Age23}); data.Add(new Person() { FirstName Kitty, LastName Brown, Age 22 }); data.Add(new Person() { FirstName Mic, LastName Brown, Age 21 }); return data; } 复制代码 保存XML文件 private void button1_Click(object sender, RoutedEventArgs e) { XmlWriterSettings xmlwriterSettings new XmlWriterSettings(); xmlwriterSettings.Indent true;//缩进 using(IsolatedStorageFile isoIsolatedStorageFile.GetUserStoreForApplication ()) { using(IsolatedStorageFileStream isoStreamiso.OpenFile(People.xml,FileMode.Create)) { XmlSerializer serializer new XmlSerializer(typeof(ListPerson)); using (XmlWriter xmlWriter XmlWriter.Create(isoStream, xmlwriterSettings)) { serializer.Serialize(xmlWriter, GeneratePersonData()); } } } } 复制代码 读取XML文件 private void button2_Click(object sender, RoutedEventArgs e) { using(IsolatedStorageFile isoIsolatedStorageFile.GetUserStoreForApplication ()) { using(IsolatedStorageFileStream isoStreamiso.OpenFile(People.xml,FileMode.Open)) { XmlSerializer serializernew XmlSerializer (typeof(ListPerson)); ListPerson data (ListPerson)serializer.Deserialize(isoStream); listBox1.ItemsSource data; } } } 复制代码 Binding ListBox ListBox Height532 HorizontalAlignmentLeft Margin0,69,0,0 NamelistBox1 VerticalAlignmentTop Width450 ListBox.ItemTemplate DataTemplate StackPanel Margin10 TextBlock Height30 NametextBlock1 Text{Binding FirstName} / TextBlock Height30 NametextBlock2 Text{Binding LastName} / TextBlock Height30 NametextBlock3 Text{Binding Age}/ /StackPanel /DataTemplate /ListBox.ItemTemplate /ListBox 复制代码 提示当进行文件操作的时候始终使用using关键字using结束后会隐式调用Disposable方法清理非托管资源。 转载于:https://www.cnblogs.com/Belling/archive/2012/11/29/2794597.html