/// <summary> /// 解析xml配置并放入Dictionary中 /// </summary> /// <param name="xmlContent">xml文本内容</param> /// <param name="rootNodeName">根节点名字</param> /// <param name="fieldname">做完key保存使用的字段名</param> /// <returns></returns> public static Dictionary <int, T> RapidParse <T>(String xmlContent, String rootNodeName, String fieldname) { Dictionary <int, T> dict = new Dictionary <int, T>(); List <String> attributesNameList = new List <String>(); RapidXmlParser xmlRapid = new RapidXmlParser(); xmlRapid.Load(xmlContent); NodeElement RootNode = xmlRapid.FirstNode(rootNodeName); NodeElement firstNode = RootNode.FirstNode(); int attribCount = firstNode.GetAttributeCount(); int nodeCount = RootNode.GetChildNodeCount(); var attribFormat = firstNode.FirstAttribute(); string str = attribFormat.GetName(); int num = 0; int keyIndex = 0; string name; while (num < attribCount) { name = attribFormat.GetName(); if (name == fieldname) { keyIndex = num; } attributesNameList.Add(name); attribFormat = attribFormat.NextAttribute(); num++; } while (firstNode.IsValid()) { T s = System.Activator.CreateInstance <T>(); string nodename = firstNode.GetName(); var attribute = firstNode.FirstAttribute(); int key = 0; for (int i = 0; i < attribCount; i++) { if (i == keyIndex) { key = attribute.GetInt(); } SetObjectValue <T>(s, attributesNameList[i], attribute.GetValue()); attribute = attribute.NextAttribute(); } try { dict.Add(key, s); } catch { throw new ArgumentException("找不到key"); } firstNode = firstNode.NextSibling(); } //Profiler.EndSample(); return(dict); }
void TestParse_Rapid() { Profiler.BeginSample("Rapid"); using (RapidXmlParser xml = new RapidXmlParser()) { xml.Load(XmlContent); } Profiler.EndSample(); }
/// <summary> /// 解析xml配置并放入list中 /// </summary> /// <param name="xmlContent">xml 文本内容</param> /// <param name="rootNodeName">根节点名字</param> /// <returns></returns> public static List <T> RapidParse <T>(string xmlContent, String rootNodeName) { //Profiler.BeginSample("RapidParse List<T>"); List <String> attributesNameList = new List <String>(); List <T> list = new List <T>(); RapidXmlParser xmlRapid = new RapidXmlParser(); xmlRapid.Load(xmlContent); NodeElement RootNode = xmlRapid.FirstNode(rootNodeName); NodeElement firstNode = RootNode.FirstNode(); int attribCount = firstNode.GetAttributeCount(); int nodeCount = RootNode.GetChildNodeCount(); var attribFormat = firstNode.FirstAttribute(); string str = attribFormat.GetName(); int num = 0; string name; while (num < attribCount) { name = attribFormat.GetName(); attributesNameList.Add(name); attribFormat = attribFormat.NextAttribute(); num++; } #if UNITY_EDITOR try { #endif num = 0; while (num < nodeCount) { T s = System.Activator.CreateInstance <T>(); var attribute = firstNode.FirstAttribute(); for (int i = 0; i < attribCount; i++) { SetObjectValue <T>(s, attributesNameList[i], attribute.GetValue()); attribute = attribute.NextAttribute(); } list.Add(s); firstNode = firstNode.NextSibling(); num++; } #if UNITY_EDITOR } catch (Exception e) { Debug.LogError(typeof(T).Name.ToString().Replace("Po", "") + ".xml表第" + (num + 3) + "行配的有问题,这个错误很严重一定要改,不改游戏会崩溃" + @" 啦啦啦德玛西亚,啦啦啦德玛西亚 配置表又配错啦,配置表又配错啦"); } #endif //Profiler.EndSample(); return(list); }
void Awake() { string path = System.IO.Path.Combine(Application.dataPath, @"Plugins/Xml/Tests/Test.xml"); Debug.Log(path); XmlContent = System.Text.Encoding.UTF8.GetString(System.IO.File.ReadAllBytes(path)); xmlRapid.Load(XmlContent); xmlSystem.LoadXml(XmlContent); xmlMono.LoadXml(XmlContent); }