日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > asp.net >内容正文

asp.net

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

發布時間:2023/11/30 asp.net 27 豆豆
生活随笔 收集整理的這篇文章主要介紹了 (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類:

  • 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=23});
  • ? ?? ?? ?? ?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 iso=IsolatedStorageFile.GetUserStoreForApplication ())
  • ? ?? ?? ?? ?{
  • ? ?? ?? ?? ?? ? using(IsolatedStorageFileStream isoStream=iso.OpenFile("People.xml",FileMode.Create))
  • ? ?? ?? ?? ?? ? {
  • ? ?? ?? ?? ?? ?? ???XmlSerializer serializer = new XmlSerializer(typeof(List<Person>));
  • ? ?? ?? ?? ?? ?? ???using (XmlWriter xmlWriter = XmlWriter.Create(isoStream, xmlwriterSettings))
  • ? ?? ?? ?? ?? ?? ???{
  • ? ?? ?? ?? ?? ?? ?? ?? ?serializer.Serialize(xmlWriter, GeneratePersonData());
  • ? ?? ?? ?? ?? ?? ???}
  • ? ?? ?? ?? ?? ? }
  • ? ?? ?? ?? ?}
  • ? ?? ???}
  • 復制代碼

    讀取XML文件:

  • private void button2_Click(object sender, RoutedEventArgs e)
  • ? ?? ???{
  • ? ?? ?? ???using(IsolatedStorageFile iso=IsolatedStorageFile.GetUserStoreForApplication ())
  • ? ?? ?? ???{
  • ? ?? ?? ?? ?? ? using(IsolatedStorageFileStream isoStream=iso.OpenFile("People.xml",FileMode.Open))
  • ? ?? ?? ?? ?? ? {
  • ? ?? ?? ?? ?? ?? ???XmlSerializer serializer=new XmlSerializer (typeof(List<Person>));
  • ? ?? ?? ?? ?? ?? ???List<Person> data = (List<Person>)serializer.Deserialize(isoStream);
  • ? ?? ?? ?? ?? ?? ???listBox1.ItemsSource = data;
  • ? ?? ?? ?? ?? ? }
  • ? ?? ?? ???}
  • ? ?? ???}
  • 復制代碼

    Binding ListBox:
  • <ListBox Height="532" HorizontalAlignment="Left" Margin="0,69,0,0" Name="listBox1" VerticalAlignment="Top" Width="450" >
  • ? ?? ?? ?? ?? ? <ListBox.ItemTemplate>
  • ? ?? ?? ?? ?? ?? ???<DataTemplate >
  • ? ?? ?? ?? ?? ?? ?? ?? ?<StackPanel Margin="10" >
  • ? ?? ?? ?? ?? ?? ?? ?? ?? ? <TextBlock Height="30"??Name="textBlock1" Text="{Binding FirstName}" />
  • ? ?? ?? ?? ?? ?? ?? ?? ?? ? <TextBlock Height="30"??Name="textBlock2" Text="{Binding LastName}"??/>
  • ? ?? ?? ?? ?? ?? ?? ?? ?? ? <TextBlock Height="30"??Name="textBlock3" Text="{Binding Age}"/>
  • ? ?? ?? ?? ?? ?? ?? ?? ?</StackPanel>
  • ? ?? ?? ?? ?? ?? ???</DataTemplate>
  • ? ?? ?? ?? ?? ? </ListBox.ItemTemplate>
  • ? ?? ?? ?? ?</ListBox>
  • 復制代碼

    提示:當進行文件操作的時候始終使用using關鍵字,using結束后會隱式調用Disposable方法,清理非托管資源。

    轉載于:https://www.cnblogs.com/Belling/archive/2012/11/29/2794597.html

    總結

    以上是生活随笔為你收集整理的(3)[wp7数据存储] WP7 IsolatedStorage系列篇——通过XmlSerializer读写XML文件 [复制链接]...的全部內容,希望文章能夠幫你解決所遇到的問題。

    如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。