加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Unity 读写XML

发布时间:2020-12-16 08:42:07 所属栏目:百科 来源:网络整理
导读:Unity 读写 XML using UnityEngine; using System.Collections; using System.Xml; using System.Collections.Generic; using System.IO; public class ReadWriteXML : MonoBehaviour { private string path; private string userID = string .Empty; privat

Unity 读写 XML

using UnityEngine;
using System.Collections;
using System.Xml;
using System.Collections.Generic;
using System.IO;

public class ReadWriteXML : MonoBehaviour {

    private string path;

    private string userID = string.Empty;
    private string userName = string.Empty;

    // Use this for initialization
    void Start () {
        path = Application.dataPath + "/XML/test.xml";
    }

    // Update is called once per frame
    void Update () {
        if (Input.GetKeyDown (KeyCode.A))
        {
            CreateXML();
        }

        if (Input.GetKeyDown (KeyCode.D))
        {
            ReadXML();
        }
    }

    private void CreateXML()
    {
        string p = Path.GetDirectoryName (path);
        Debug.LogError (p);
        if (!Directory.Exists (p))
        {
            Directory.CreateDirectory(p);
        }

        if (File.Exists (path))
        {
            Directory.Delete(path);
        }

        // 新建 XML 实例
        XmlDocument xmlDoc = new XmlDocument();

        // 创建跟节点,最上层节点
        XmlElement root = xmlDoc.CreateElement("Root");
        xmlDoc.AppendChild(root);

        // 二级节点
        XmlElement user = xmlDoc.CreateElement("user");
        root.AppendChild(user);

        // 三级节点
        XmlElement userID = xmlDoc.CreateElement("userID");
        userID.InnerText = "123";
        user.AppendChild(userID);

        // 三级节点
        XmlElement userName = xmlDoc.CreateElement("userName");
        userName.InnerText = "hehe";
        user.AppendChild(userName);

        // 二级节点
        XmlElement npcList = xmlDoc.CreateElement("npcList");
        root.AppendChild(npcList);

        // 二级节点的两个属性
        npcList.SetAttribute("count","5");
        npcList.SetAttribute("group","ss");

        for (int i = 0; i < 5; i ++)
        {
            // 三级节点
            XmlElement npc = xmlDoc.CreateElement("npc");
            // 二节点的两个属性
            npc.SetAttribute("npcID",i.ToString());
            npc.SetAttribute("speed",(i * 10).ToString());
            npc.SetAttribute("life",(i * 100).ToString());

            npcList.AppendChild(npc);
        }

        //将XML 文件保存到本地
        xmlDoc.Save(path);
    }


    private void ReadXML()
    {
        // 判断文件不存在返回
        if (!File.Exists (path))
        {
            return;
        }

        // 新建 XML 实例
        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.Load(path);

        // 获取二级节点 user
        XmlNode node = xmlDoc.SelectSingleNode("Root/user");

        // 获取二级节点下的所有三级节点
        XmlNodeList nodeList = node.ChildNodes;

        // 遍历所有的三级节点
        for (int i = 0; i < nodeList.Count; ++i)
        {
            XmlNode _node = nodeList[i];

            // 获取二级节点名为 userID
            if (_node.Name == "userID")
            {
                Debug.LogError("userID " + _node.InnerText);
            }

            // 获取二级节点名为 userName
            if (_node.Name == "userName")
            {
                Debug.LogError ("userName " + _node.InnerText);
            }
        }

        // 获取二级节点 npcList
        XmlNode npcListNode = xmlDoc.SelectSingleNode("Root/npcList");

        // 遍历二级节点的所有属性
        for (int j = 0; j < npcListNode.Attributes.Count; ++j)
        {
            XmlAttribute xa = npcListNode.Attributes[j];
            // 获取属性为 count 的值
            if (xa.Name == "count")
            {
                Debug.LogError("count " + xa.Value);
            }

            // 获取属性为 group 的值
            if (xa.Name == "group")
            {
                Debug.LogError("group " + xa.Value);
            }
        }

        // 获取节点的所有子节点列表
        XmlNodeList npcNodeList = npcListNode.ChildNodes;

        // 遍历二级节点
        for (int i = 0; i < npcNodeList.Count; ++i)
        {
            // 标记当前节点
            XmlNode currentNode = npcNodeList[i];

            // 遍历当前节点的所有属性
            for (int j = 0; j < currentNode.Attributes.Count; ++j)
            {
                XmlAttribute xa = currentNode.Attributes[j];  

                if (xa.Name == "npcID")
                {
                    Debug.LogError("npcID " + xa.Value);
                }

                if (xa.Name == "speed")
                {
                    Debug.LogError("speed " + xa.Value);
                }

                if (xa.Name == "life")
                {
                    Debug.LogError("life " + xa.Value);
                }
            }
        }
    }
}

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读