Exemple #1
0
        private static void SaveXML(object config, string path)
        {
            var root = new System.Security.SecurityElement("root");

            root.AddChild(new System.Security.SecurityElement("record"));
            var xml   = root.Children[0] as System.Security.SecurityElement;
            var props = config.GetType().GetProperties();

            foreach (var item in props)
            {
                if (item.Name.Contains("GuideTimes"))
                {
                    //dictonary
                    var    temp  = item.GetGetMethod().Invoke(config, null);
                    string value = "";
                    foreach (var v in temp as Dictionary <ulong, string> )
                    {
                        value = value + v.Key.ToString() + ":" + v.Value + ",";
                    }
                    xml.AddChild(new System.Security.SecurityElement(item.Name, value));
                }
                else
                {
                    var value = item.GetGetMethod().Invoke(config, null);
                    xml.AddChild(new System.Security.SecurityElement(item.Name, value.ToString()));
                }
            }
            XMLParser.SaveText(path, root.ToString());
        }
Exemple #2
0
        private static void SaveXMLList <T>(string path, List <T> data, string attrName = "record")
        {
            var root  = new System.Security.SecurityElement("root");
            var i     = 0;
            var props = typeof(T).GetProperties();

            foreach (var item in data)
            {
                var xml = new System.Security.SecurityElement(attrName);
                foreach (var prop in props)
                {
                    var    type   = prop.PropertyType;
                    String result = String.Empty;
                    object obj    = prop.GetGetMethod().Invoke(item, null);
                    //var obj = prop.GetValue(item, null);
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                    {
                        result = typeof(Utils).GetMethod("PackMap")
                                 .MakeGenericMethod(type.GetGenericArguments())
                                 .Invoke(null, new object[] { obj, ':', ',' }).ToString();
                    }
                    else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
                    {
                        result = typeof(Utils).GetMethod("PackList")
                                 .MakeGenericMethod(type.GetGenericArguments())
                                 .Invoke(null, new object[] { obj, ',' }).ToString();
                    }
                    else
                    {
                        result = obj.ToString();
                    }
                    xml.AddChild(new System.Security.SecurityElement(prop.Name, result));
                }
                root.AddChild(xml);
                i++;
            }
            XMLParser.SaveText(path, root.ToString());
        }
        // 可以扩展成递归模式,多个类型嵌套的
        public static void SaveXML <T>(string path, List <T> data)
        {
            var    root     = new System.Security.SecurityElement("root");
            string attrName = typeof(T).Name;
            var    props    = typeof(T).GetProperties();
            var    keyProp  = props[0];

            foreach (var prop in props)
            {
                if (XmlData.mKeyFieldName == prop.Name)
                {
                    keyProp = prop;
                    break;
                }
            }
            foreach (var item in data)
            {
                // 下面在LoadXML时,key id必须先加载,所以这里第一个创建 id
                var    xml = new System.Security.SecurityElement(attrName);
                object obj = keyProp.GetGetMethod().Invoke(item, null);
                xml.AddChild(new System.Security.SecurityElement(keyProp.Name, obj.ToString()));
                foreach (var prop in props)
                {
                    if (prop == keyProp)
                    {
                        continue;
                    }
                    var    type   = prop.PropertyType;
                    String result = String.Empty;
                    obj = prop.GetGetMethod().Invoke(item, null);
                    if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(Dictionary <,>))
                    {
                        result = typeof(XmlFileUtils).GetMethod("PackMap")
                                 .MakeGenericMethod(type.GetGenericArguments())
                                 .Invoke(null, new object[] { obj, KEY_VALUE_SPRITER, MAP_SPRITER }).ToString();
                    }
                    else if (type.IsGenericType && type.GetGenericTypeDefinition() == typeof(List <>))
                    {
                        result = typeof(XmlFileUtils).GetMethod("PackList")
                                 .MakeGenericMethod(type.GetGenericArguments())
                                 .Invoke(null, new object[] { obj, LIST_SPRITER }).ToString();
                    }
                    else if (type.BaseType == typeof(Enum))
                    {
                        Type underType = Enum.GetUnderlyingType(type);
                        if (underType == typeof(Int32))
                        {
                            obj = EnumConvertUtils.EnumToInt(obj) + "";
                        }
                        result = obj.ToString();
                    }
                    else
                    {
                        result = obj.ToString();
                    }
                    xml.AddChild(new System.Security.SecurityElement(prop.Name, result));
                }
                root.AddChild(xml);
            }
            XMLParser.SaveText(path, root.ToString());
        }