zoukankan      html  css  js  c++  java
  • Unity3D ConfigMan.cs For XML File

    using System;
    using System.Collections.Generic;
    using System.IO;
    using System.Linq;
    using System.Text;
    using System.Threading.Tasks;
    using System.Xml;
    using UnityEngine;
    
    namespace Assets.Scripts.Models
    {
        public class ConfigMan
        {
            /// <summary>
            /// 读取指定节点的属性
            /// </summary>
            /// <param name="nodeName">节点名</param>
            /// <param name="attributeName">属性名</param>
            /// <returns></returns>
            public static string ReadNode(string nodeName,string attributeName) {
                //Debug.Log("Application.dataPath:" + Application.dataPath);
                //Debug.Log("Application.streamingAssetsPath:" + Application.streamingAssetsPath);
                //Debug.Log("Application.consoleLogPath:" + Application.consoleLogPath);
                //Debug.Log("Application.temporaryCachePath:" + Application.temporaryCachePath);
    
    
                var path = Application.dataPath + "/Configrations/MyConfig.xml";
                var path1 = Application.streamingAssetsPath + "/Configrations/MyConfig.xml";
    
                //StreamReader sr = new StreamReader(newPath, Encoding.UTF8);
    
                /**判断文件是否存在**/
                if (File.Exists(path))
                {
                    XmlDocument doc = new XmlDocument();
                    doc.Load(path);//载入对应的配置文件
                    XmlNodeList configs = doc.SelectSingleNode("Root").ChildNodes;//读取根节点下所有子节点
    
                    foreach (XmlElement item in configs)
                    {
                        if (item.HasAttribute(attributeName) && item.Name == nodeName)
                        {
                            return item.GetAttribute(attributeName);
                        }
                    }
                    return "";
                }
                else {
                    return "";
                }
    
            }
            /// <summary>
            /// 创建新的节点
            /// </summary>
            public void CreateNode() { }
    
            /// <summary>
            /// 更新已有节点属性值
            /// </summary>
            /// <param name="name">节点名称</param>
            /// <param name="attributeName">要修改的属性名称</param>
            /// <param name="value">属性值</param>
            public static void UpdateNode(string name, string attributeName, string value) {
                string path = Application.dataPath + "/Configrations/MyConfig.xml";
                XmlDocument doc = new XmlDocument();
                doc.Load(path);//载入xml文件
                XmlNodeList nodes=doc.SelectSingleNode("Root").ChildNodes;//获取根节点下所有子节点
                try
                {
                    if (nodes.Count > 0) {
                        //如果目录下有内容
                        foreach (XmlElement e in nodes) {
                            if (e.Name == name&& e.HasAttribute(attributeName)) {
                                e.SetAttribute(attributeName, value);//update the value
                                doc.Save(path);//save file
                                break;
                            }
                        }
                    }
                }
                catch (Exception ex) {
                    Debug.Log(ex.GetBaseException());
                }
            }
        }
    }
    

      

  • 相关阅读:
    windows程序设计笔记(11)
    windows程序设计笔记(8)
    windows程序设计笔记(9)
    windows程序设计笔记(7)
    [转]学习训练方法
    CA面试题
    windows程序设计笔记(10)
    C#线程系列(3):线程池和文件下载服务器
    Linq 使用小结
    C#线程系列(2):Thread类的应用
  • 原文地址:https://www.cnblogs.com/nick-jd/p/12421227.html
Copyright © 2011-2022 走看看