예제 #1
0
        private PlistElementDict GetOrCreateUniqueDictElementInArray(PlistElementArray root)
        {
            PlistElementDict r;

            if (root.values.Count == 0)
            {
                r = root.values[0] as PlistElementDict;
            }
            else
            {
                r = new PlistElementDict();
                root.values.Add(r);
            }
            return(r);
        }
예제 #2
0
        private PlistElementString GetOrCreateStringElementInArray(PlistElementArray root, string value)
        {
            PlistElementString r = null;
            var c     = root.values.Count;
            var exist = false;

            for (var i = 0; i < c; i++)
            {
                if (root.values[i] is PlistElementString && (root.values[i] as PlistElementString).value == value)
                {
                    r     = root.values[i] as PlistElementString;
                    exist = true;
                }
            }
            if (!exist)
            {
                r = new PlistElementString(value);
                root.values.Add(r);
            }
            return(r);
        }
예제 #3
0
        private static PlistElement ReadElement(XElement xml)
        {
            switch (xml.Name.LocalName)
            {
            case "dict":
            {
                List <XElement> children = xml.Elements().ToList();
                var             el       = new PlistElementDict();

                if (children.Count % 2 == 1)
                {
                    throw new Exception("Malformed plist file");
                }

                for (int i = 0; i < children.Count - 1; i++)
                {
                    if (children[i].Name != "key")
                    {
                        throw new Exception("Malformed plist file");
                    }
                    string key      = GetText(children[i]).Trim();
                    var    newChild = ReadElement(children[i + 1]);
                    if (newChild != null)
                    {
                        i++;
                        el[key] = newChild;
                    }
                }
                return(el);
            }

            case "array":
            {
                List <XElement> children = xml.Elements().ToList();
                var             el       = new PlistElementArray();

                foreach (var childXml in children)
                {
                    var newChild = ReadElement(childXml);
                    if (newChild != null)
                    {
                        el.values.Add(newChild);
                    }
                }
                return(el);
            }

            case "string":
                return(new PlistElementString(GetText(xml)));

            case "integer":
            {
                int r;
                if (int.TryParse(GetText(xml), out r))
                {
                    return(new PlistElementInteger(r));
                }
                return(null);
            }

            case "true":
                return(new PlistElementBoolean(true));

            case "false":
                return(new PlistElementBoolean(false));

            default:
                return(null);
            }
        }