private static object ElementToDict()

in Darabonba/Utils/XmlUtils.cs [174:228]


        private static object ElementToDict(XmlNode element, Dictionary<string, object> nodeDict)
        {
            XmlNodeList elements = element.ChildNodes;
            if (elements.Count == 0 || (elements.Count == 1 && !elements[0].HasChildNodes))
            {
                string context = string.IsNullOrEmpty(element.InnerText.Trim()) ? null : element.InnerText.Trim();
                if (nodeDict != null)
                {
                    nodeDict.Add(element.Name, context);
                }
                return context;
            }
            else
            {
                Dictionary<string, object> subDict = new Dictionary<string, object>();
                if (nodeDict != null)
                {
                    nodeDict.Add(element.Name, subDict);
                }
                foreach (XmlNode subNode in elements)
                {
                    if (subDict.ContainsKey(subNode.Name))
                    {
                        object o = subDict[subNode.Name];
                        Type type = o.GetType();
                        if (typeof(IList).IsAssignableFrom(type))
                        {
                            ((IList)o).Add(ElementToDict(subNode, null));
                        }
                        else if (typeof(IDictionary).IsAssignableFrom(type))
                        {
                            List<object> list = new List<object>();
                            Dictionary<string, object> remove = (Dictionary<string, object>)subDict[subNode.Name];
                            subDict.Remove(subNode.Name);
                            list.Add(remove);
                            list.Add(ElementToDict(subNode, null));
                            subDict.Add(subNode.Name, list);
                        }
                        else
                        {
                            List<object> list = new List<object>();
                            list.Add(o);
                            subDict.Remove(subNode.Name);
                            list.Add(ElementToDict(subNode, null));
                            subDict.Add(subNode.Name, list);
                        }
                    }
                    else
                    {
                        ElementToDict(subNode, subDict);
                    }
                }
                return subDict;
            }
        }