示例#1
0
    public static bool LoadXmlKeyValueStringToDict <T>(string path, string keyValue, ref Dictionary <string, T> dict) where T : class, new()
    {
        XmlElement root = null;

        if (!XMLLoader.LoadRootElement(PATH_DIR + path, out root))
        {
            Debug.LogError("Failed to load" + path + "Table!");
            return(false);
        }

        if (dict == null)
        {
            dict = new Dictionary <string, T>();
        }
        else
        {
            dict.Clear();
        }

        for (int dataIdx = 0; dataIdx < root.ChildNodes.Count; ++dataIdx)
        {
            XmlElement  node     = root.ChildNodes[dataIdx] as XmlElement;
            FieldInfo[] fieldArr = typeof(T).GetFields();
            T           data     = new T();

            string strValue = node.GetAttribute(keyValue);
            if (string.IsNullOrEmpty(strValue))
            {
                Debug.LogError("Failed to Dict Load key Name is :" + strValue);
                return(false);
            }
            else
            {
                for (int i = 0; i < fieldArr.Length; ++i)
                {
                    FieldInfo fi    = fieldArr[i];
                    string    value = node.GetAttribute(fi.Name);
                    if (!string.IsNullOrEmpty(value))
                    {
                        SetValue <T>(fi, data, value);
                    }
                }

                dict.Add(strValue, data);
            }
        }

        return(true);
    }
示例#2
0
    public static bool LoadXmlToList <T>(string path, ref List <T> list) where T : class, new()
    {
        XmlElement root = null;

        if (!XMLLoader.LoadRootElement(PATH_DIR + path, out root))
        {
            Debug.LogError("Failed to load" + path + "Table!");
            return(false);
        }

        if (list == null)
        {
            list = new List <T>();
        }
        else
        {
            list.Clear();
        }

        for (int dataIdx = 0; dataIdx < root.ChildNodes.Count; ++dataIdx)
        {
            XmlElement  node     = root.ChildNodes[dataIdx] as XmlElement;
            FieldInfo[] fieldArr = typeof(T).GetFields();
            T           data     = new T();
            for (int i = 0; i < fieldArr.Length; ++i)
            {
                FieldInfo fi    = fieldArr[i];
                string    value = node.GetAttribute(fi.Name);
                if (!string.IsNullOrEmpty(value))
                {
                    SetValue <T>(fi, data, value);
                }
            }

            list.Add(data);
        }

        return(true);
    }