private static Dictionary GetDictFromXml()

in Darabonba/Utils/XmlUtils.cs [67:124]


        private static Dictionary<string, object> GetDictFromXml(XmlNode element, Type type)
        {
            Dictionary<string, object> nodeDict = new Dictionary<string, object>();
            PropertyInfo[] properties = type.GetProperties();
            for (int i = 0; i < properties.Length; i++)
            {
                PropertyInfo p = properties[i];
                Type propertyType = p.PropertyType;
                NameInMapAttribute attribute = p.GetCustomAttribute(typeof(NameInMapAttribute)) as NameInMapAttribute;
                string realName = attribute == null ? p.Name : attribute.Name;
                XmlNodeList node = element.SelectNodes(realName);

                if (node != null && node.Count > 0)
                {
                    int count = (node[0].OuterXml.Length - node[0].OuterXml.Replace(realName, "").Length) / realName.Length;
                    if (count > 1)
                    {
                        if (typeof(IList).IsAssignableFrom(propertyType))
                        {
                            Type innerPropertyType = propertyType.GetGenericArguments()[0];
                            if (typeof(Model).IsAssignableFrom(innerPropertyType))
                            {
                                IList dicList = new List<Dictionary<string, object>>();
                                for (int j = 0; j < node.Count; j++)
                                {
                                    dicList.Add(GetDictFromXml(node.Item(j), innerPropertyType));
                                }
                                nodeDict.Add(realName, dicList);
                            }
                            else
                            {
                                var dicList = (IList)Activator.CreateInstance(propertyType);
                                for (int j = 0; j < node.Count; j++)
                                {
                                    var value = mapObj(innerPropertyType, node.Item(j).InnerText);
                                    dicList.Add(value);
                                }
                                nodeDict.Add(realName, dicList);
                            }
                        }
                        else if (typeof(Model).IsAssignableFrom(propertyType))
                        {
                            nodeDict.Add(realName, GetDictFromXml(node.Item(0), propertyType));
                        }
                        else
                        {
                            string value = node.Item(0).InnerText;
                            nodeDict.Add(realName, mapObj(propertyType, value));
                        }
                    }
                }
                else
                {
                    nodeDict.Add(realName, null);
                }
            }
            return nodeDict;
        }