之前我貼過一段很簡單的C#生成plist文件的代碼,但是反過來如果要讀取plist文件呢?有沒有實現這樣功能的類庫呢?答案是肯定的,下午在網上找了一個iphone-plist-net庫試用了一下感覺很是方便,看代碼:
[csharp]
//寫入
var dic = new PListDict();
dic["name"] = new PListString("WangQiuyun");
dic["age"] = new PListInteger(25);
dic["address"] = new PListString("北京海澱區永泰莊");
var arr = new PListArray();
arr.Add(new PListInteger(1));
arr.Add(new PListInteger(2));
arr.Add(new PListInteger(3));
arr.Add(new PListInteger(4));
arr.Add(new PListInteger(5));
dic["array"] = arr;
var myRoot = new PListRoot();
myRoot.Root = dic;
myRoot.Save("mytest.plist", PListFormat.Xml);
myRoot.Save("mytest.bplist", PListFormat.Binary);
[csharp]
//讀取
PListRoot root = PListRoot.Load(@"mytest.plist");
PListDict dic = (PListDict)root.Root;
PListString name = (PListString)dic["name"];
listBox1.Items.Add(name.Value+" 類型:"+name.Tag);
PListInteger age = (PListInteger)dic["age"];
listBox1.Items.Add(age.Value + " 類型:" + age.Tag);
PListString address = (PListString)dic["address"];
listBox1.Items.Add(address.Value + " 類型:" + address.Tag);
PListArray arr = (PListArray)dic["array"];
listBox1.Items.Add(((PListInteger)arr[0]).Value);
listBox1.Items.Add(((PListInteger)arr[1]).Value);
listBox1.Items.Add(((PListInteger)arr[2]).Value);
listBox1.Items.Add(((PListInteger)arr[3]).Value);
listBox1.Items.Add(((PListInteger)arr[4]).Value);
[csharp]
//讀寫
PListRoot root = PListRoot.Load("mytest.plist");
using (MemoryStream memStream = new MemoryStream())
{
root.Save(memStream, PListFormat.Xml);
textBox1.Text = Encoding.UTF8.GetString(memStream.ToArray());
}
root.Save("com.apple.springboard.XML.plist", PListFormat.Xml);
root.Save("com.apple.springboard.BIN.plist", PListFormat.Binary);