Unity 3D 持久化数据 XML

XML在开发中使用也很频繁,此时要以标签的形式来组织数据结构。

C#提供了创建 解析 修改 和查询等方法,可以很方便的操作它。

//--创建XML

using System.IO;
using System.Xml;
using UnityEngine;

public class CreateXML : MonoBehaviour
{
    private void Start()
    {
        //--创建XML
        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xmlDoc.AppendChild(xmlDeclaration);

        //--在节点中写入数据
        XmlElement root = xmlDoc.CreateElement("XmlRoot");
        xmlDoc.AppendChild(root);

        XmlElement group = xmlDoc.CreateElement("Group");
        group.SetAttribute("name", "老司机");
        group.SetAttribute("age", "666");
        root.AppendChild(group);

        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter))
            {
                xmlDoc.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                Debug.Log(stringWriter.ToString());
                //RecordUtil.Set("CreateXmlTest", stringWriter.ToString());
            }
        }
    }
}

首先需要引入System.Xml命名空间,接着创建XmlDocument对象,然后就可以给XML节点添加数据了。

//--读取与修改

using System.IO;
using System.Xml;
using UnityEngine;

public class ReadAndChangeXml : MonoBehaviour
{
    private void Start()
    {
        string xml = "<?xml version=\"1.0\" encoding=\"UTF-8\"?><XmlRoot><Group name=\"老司机\" age=\"666\" /></XmlRoot>";

        XmlDocument xmlDoc = new XmlDocument();
        xmlDoc.LoadXml(xml);

        XmlNode nodes = xmlDoc.SelectSingleNode("XmlRoot");
        foreach (XmlNode node in nodes.ChildNodes)
        {
            string name = node.Attributes["name"].Value;
            string age = node.Attributes["age"].Value;
            Debug.LogFormat("name :{0},age :{1}", name, age);

            node.Attributes["age"].Value = "888";
        }

        using (StringWriter stringWriter = new StringWriter())
        {
            using (XmlTextWriter xmlTextWriter = new XmlTextWriter(stringWriter))
            {
                xmlDoc.WriteTo(xmlTextWriter);
                xmlTextWriter.Flush();
                Debug.Log(stringWriter.ToString());
            }
        }
    }
}

创建XmlDocument对象,读取XML字符串,通过循环可以遍历所有节点对他们进行修改。

//--写入读取XML文件

XmlDocument类也提供了从文件中读取XML,或将XML写入本地路径的方法。

using System.IO;
using System.Xml;
using UnityEditor;
using UnityEngine;

public class WriteAndLoadXmlMyTools
{
    [MenuItem("MyXml/WriteXml", false, 0)]
    static void WriteXml()
    {
        string xmlPath = Path.Combine(Application.dataPath, "Resources/MyXml/test.xml");
        if (File.Exists(xmlPath))
        {
            File.Delete(xmlPath);
        }

        XmlDocument xmlDoc = new XmlDocument();
        XmlDeclaration xmlDeclaration = xmlDoc.CreateXmlDeclaration("1.0", "UTF-8", null);
        xmlDoc.AppendChild(xmlDeclaration);

        XmlElement root = xmlDoc.CreateElement("XmlRoot");
        xmlDoc.AppendChild(root);

        for (int i = 0; i < 5; i++)
        {
            XmlElement group = xmlDoc.CreateElement("Group");
            group.SetAttribute("id", i.ToString());
            group.SetAttribute("name", "老司机" + i.ToString());
            group.SetAttribute("age", (18 + i).ToString());
            root.AppendChild(group);
        }
        xmlDoc.Save(xmlPath);
        AssetDatabase.Refresh();
    }

    [MenuItem("MyXml/LoadXml", false, 1)]
    static void LoadXml()
    {
        string xmlPath = Path.Combine(Application.dataPath, "Resources/MyXml/test.xml");
        if (File.Exists(xmlPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(xmlPath);

            XmlNode nodes = xmlDoc.SelectSingleNode("XmlRoot");
            foreach (XmlNode node in nodes.ChildNodes)
            {
                string id = node.Attributes["id"].Value;
                string name = node.Attributes["name"].Value;
                string age = node.Attributes["age"].Value;
                Debug.LogFormat("id={0},name={1},age={2}", id, name, age);
            }
        }
    }
}

 写入

读取

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值