Skip to content

Instantly share code, notes, and snippets.

@darrelmiller
Created January 7, 2012 15:52
Show Gist options
  • Save darrelmiller/1575103 to your computer and use it in GitHub Desktop.
Save darrelmiller/1575103 to your computer and use it in GitHub Desktop.
Extremely naive Apple Property List parser
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Xml.Linq;
using System.Linq;
namespace LinqApplePropertyList {
class Program {
static void Main(string[] args) {
var doc = XDocument.Load("Data.xml");
var plists = doc.ReadAsPLists();
}
}
public static class PListExtensions {
public static List<PList> ReadAsPLists(this XDocument doc) {
return (from p in doc.Elements("plist")
select new PList() {
Arrays = p.ReadAsArrays()
}).ToList();
}
public static List<PLArray> ReadAsArrays(this XElement pListElement) {
return (from a in pListElement.Elements("array")
select new PLArray() {
Dicts = a.ReadAsDicts()
}).ToList();
}
public static List<PLDict> ReadAsDicts(this XElement dictElement) {
return (from d in dictElement.Elements("dict")
select new PLDict() {
KeyValuePairs = d.ReadAsKeyValuePairs()
}).ToList();
}
public static List<PLKeyValuePair> ReadAsKeyValuePairs(this XElement arrayElement) {
return (from k in arrayElement.Elements("key")
select new PLKeyValuePair() {
Key = k.Value,
Value = k.ElementsAfterSelf().First().ReadAsValue()
}).ToList();
}
public static PLValue ReadAsValue(this XElement valueElement) {
var plValue = new PLValue();
if (valueElement.Name == "string") {
plValue.StringValue = valueElement.Value;
}
return plValue;
}
}
public class PList {
public List<PLArray> Arrays;
}
public class PLArray {
public List<PLDict> Dicts;
}
public class PLDict {
public List<PLKeyValuePair> KeyValuePairs;
}
public class PLKeyValuePair {
public string Key;
public PLValue Value;
}
public class PLValue {
public String StringValue;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment