C# 解析Xml文件

来源:互联网 发布:caffe 图片预处理 编辑:程序博客网 时间:2024/06/05 06:33
 private void LoadXMLDocument(string fileName, string xml)        {            XmlDocument xmlDoc = new XmlDocument();            XmlReaderSettings setting = new XmlReaderSettings();            setting.IgnoreComments = true;            setting.IgnoreWhitespace = true;            MemoryStream stream = new MemoryStream(Encoding.UTF8.GetBytes(xml));            XmlReader reader = XmlReader.Create(stream, setting);            xmlDoc.Load(reader);            XmlNode node = xmlDoc.FirstChild;            do            {                node = node.NextSibling;            }            while (node.NodeType == XmlNodeType.Comment);            string type = node.Attributes["type"].Value;            System.Reflection.Assembly assembly = System.Reflection.Assembly.GetExecutingAssembly();            Type refType = assembly.GetType("Core.Template." + type);            if (refType != null)            {                XmlNodeList nodeList = node.ChildNodes;                List<PrototypeData> dataList = null;                for (int i = 0; i < nodeList.Count; i++)                {                    XmlNode childNode = nodeList[i];                    if (childNode.NodeType == XmlNodeType.Comment)                    {                        continue;                    }                    PrototypeData prototypeData = System.Activator.CreateInstance(refType) as PrototypeData;                    prototypeData.LoadData(childNode);                    if (dicData.ContainsKey(prototypeData.mPrototypeID) == false)                    {                        dicData.Add(prototypeData.mPrototypeID, prototypeData);                        if (!mAllPrototyByType.TryGetValue(type, out dataList))                        {                            dataList = new List<PrototypeData>();                            mAllPrototyByType.Add(type, dataList);                        }                        dataList.Add(prototypeData);                    }                    else                    {                        GameDebug.LogError(fileName + "表有重复ID=" + prototypeData.mPrototypeID);                    }                }            }        }


0 0
原创粉丝点击