(Unity)XML文件读写与IO文件操作类使用介绍
using System.Xml; //xml文件操作命名空间
#region 写入操作
    void WriteXMLFile(string _fileName)
    {
        XmlDocument doc = new XmlDocument();           //创建文档对象
        var root = doc.CreateElement("root");                  //创建 root 标签
        doc.AppendChild(root);                                        //root标签添加到文档对象
        var hero = doc.CreateElement("hero");                //创建 root 下hero 子标签
        root.AppendChild(hero);                                      //添加到 root中    
        
        hero.SetAttribute("name", "张三");                      //填写数据到标签 
        doc.Save(_fileName);                                         //保存文件
    }
    #endregion
 #region 读取操作
    void ReadXMLFile(string _url)
    {
        
        var doc = new XmlDocument();                                         //创建文档对象
        doc.Load(_url);                                                                  //加载xml地址 
        var root = doc.SelectSingleNode("root") as XmlElement;  //读取 root标签
        foreach (XmlElement hero in root.ChildNodes)                 //遍历所有 英雄标签
        {
            string str = hero.GetAttribute("name");                              //读取数据到字符串
        }
    }
    #endregion
IO文件操作类使用介绍
using System.IO; //IO文件流操作命名空间
 if (!Directory.Exists(url))//判断文件夹是否存在
        {
            //不存在则创建该目录
            Directory.CreateDirectory(url);
        }
 //判断 文件存在否
            if (File.Exists(Application.dataPath + "/XML/HeroInfo.xml"))
            {
                //删除文件
                File.Delete(Application.dataPath + "/XML/HeroInfo.xml");
                //创建文件
                File.Create(Application.dataPath + "/XML/HeroInfo.xml");
                }
            }
 
                     
                    
                 
                    
                 
                
            
         
 
         浙公网安备 33010602011771号
浙公网安备 33010602011771号