之前我贴过一段很简单的C#生成plist文件的代码,但是反过来如果要读取plist文件呢?有没有实现这样功能的类库呢?答案是肯定的,下午在网上找了一个iphone-plist-net库试用了一下感觉很是方便,看代码:
//写入 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);
//读取 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);
//读写 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);
iphone-plist-net库及示例程序都在我的资源:http://download.csdn.net/detail/wangqiuyun/4541196