Unity 读写XML

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);
                }
            }
        }
    }
}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐


php输出xml格式字符串
J2ME Mobile 3D入门教程系列文章之一
XML轻松学习手册
XML入门的常见问题(一)
XML入门的常见问题(三)
XML轻松学习手册(2)XML概念
xml文件介绍及使用
xml编程(一)-xml语法
XML文件结构和基本语法
第2章 包装类
XML入门的常见问题(二)
Java对象的强、软、弱和虚引用
JS解析XML文件和XML字符串详解
java中枚举的详细使用介绍
了解Xml格式
XML入门的常见问题(四)
深入SQLite多线程的使用总结详解
PlayFramework完整实现一个APP(一)
XML和YAML的使用方法
XML轻松学习总节篇