Exemple #1
0
        /// <summary>
        /// Parses a node in the XML structure and returns the corresponding NSObject
        /// </summary>
        /// <returns>The corresponding NSObject.</returns>
        /// <param name="n">The XML node.</param>
        static NSObject ParseObject(XElement n, XmlOriginFactory originFactory)
        {
            var xmlOrigin = originFactory.GetOrigin(n);

            if (n.Name.LocalName.Equals("dict"))
            {
                NSDictionary    dict     = new NSDictionary(xmlOrigin);
                List <XElement> children = n.Elements().ToList();
                for (int i = 0; i < children.Count; i += 2)
                {
                    XElement key = children[i];
                    XElement val = children[i + 1];

                    string keyString = GetNodeTextContents(key);

                    dict.Add(keyString, ParseObject(val, originFactory));
                }
                return(dict);
            }
            if (n.Name.LocalName.Equals("array"))
            {
                List <XElement> children = n.Elements().ToList();
                NSArray         array    = new NSArray(children.Count, xmlOrigin);
                for (int i = 0; i < children.Count; i++)
                {
                    array.Add(ParseObject(children[i], originFactory));
                }
                return(array);
            }
            if (n.Name.LocalName.Equals("true"))
            {
                return(new NSNumber(true, xmlOrigin));
            }
            if (n.Name.LocalName.Equals("false"))
            {
                return(new NSNumber(false, xmlOrigin));
            }
            if (n.Name.LocalName.Equals("integer"))
            {
                return(new NSNumber(GetNodeTextContents(n), NumberType.Integer, xmlOrigin));
            }
            if (n.Name.LocalName.Equals("real"))
            {
                return(new NSNumber(GetNodeTextContents(n), NumberType.Real, xmlOrigin));
            }
            if (n.Name.LocalName.Equals("string"))
            {
                return(new NSString(GetNodeTextContents(n), xmlOrigin));
            }
            if (n.Name.LocalName.Equals("data"))
            {
                return(new NSData(GetNodeTextContents(n), xmlOrigin));
            }
            return(n.Name.LocalName.Equals("date") ? new NSDate(GetNodeTextContents(n), xmlOrigin) : null);
        }
Exemple #2
0
        /// <summary>
        /// Parses the XML document by generating the appropriate NSObjects for each XML node.
        /// </summary>
        /// <returns>The root NSObject of the property list contained in the XML document.</returns>
        /// <param name="doc">The XML document.</param>
        static NSObject ParseDocument(XDocument doc, XmlOriginFactory originFactory)
        {
            var docType = doc.Nodes().OfType <XDocumentType>().SingleOrDefault();

            if (docType == null)
            {
                if (!doc.Root?.Name?.LocalName?.Equals("plist") ?? false)
                {
                    throw new XmlException("The given XML document is not a property list.");
                }
            }
            else if (!docType.Name.Equals("plist"))
            {
                throw new XmlException("The given XML document is not a property list.");
            }

            XElement rootNode;

            if (doc.Root?.Name?.LocalName?.Equals("plist") ?? false)
            {
                //Root element wrapped in plist tag
                List <XElement> rootNodes = doc.Root.Elements().ToList();;
                if (rootNodes.Count == 0)
                {
                    throw new PropertyListFormatException("The given XML property list has no root element!");
                }
                if (rootNodes.Count == 1)
                {
                    rootNode = rootNodes[0];
                }
                else
                {
                    throw new PropertyListFormatException("The given XML property list has more than one root element!");
                }
            }
            else
            {
                //Root NSObject not wrapped in plist-tag
                rootNode = doc.Root;
            }

            return(ParseObject(rootNode, originFactory));
        }