Unity-XML文件创建、读取、删除、更新、添加

该博客详细介绍了如何在 Unity 中使用 C# 对 XML 文件进行操作,包括创建 XML 文件、添加新节点、读取节点信息、更新节点属性以及删除节点。示例代码涵盖了从基础的文件操作到复杂的节点操作,是 Unity 开发者处理 XML 数据的实用教程。
摘要由CSDN通过智能技术生成

Unity-XML文件创建、读取、删除、更新、添加

1、XML文件----创建

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

public class CreateXML :MonoBehaviour{    
    public string localPath;           //创建路径
    public static CreateXML instance;  //单例
    private void Awake()
    {
        instance=this;
    }
    //文件创建
    public void Create_XML()
    {
        localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
        if (!File.Exists(localPath))
        {
            XmlDocument xml = new XmlDocument();
            XmlDeclaration xmldecl = xml.CreateXmlDeclaration("1.0", "UTF-8", "");//设置xml文件编码格式为UTF-8
            XmlElement root = xml.CreateElement("Data");//创建根节点
            XmlElement info = xml.CreateElement("Info");//创建子节点
            info.SetAttribute("Name", "小王");//创建子节点属性名和属性值
            info.SetAttribute("Age", "28");
            info.SetAttribute("Phone", "12345678");
            root.AppendChild(info);//将子节点按照创建顺序,添加到xml
            xml.AppendChild(root);
            xml.Save(localPath);//保存xml到路径位置
            Debug.Log("创建XML成功!");
        }
    }  
}

2、XML文件----添加

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

public class WriteINXML : MonoBehaviour {

    public string localPath;

    private void Start()
    {
        AddXML();
    }
    void AddXML()
    {
        localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
        if (File.Exists(localPath))
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(localPath);//加载xml文件
            XmlNode root = xml.SelectSingleNode("Data");//获取根节点
            XmlElement info = xml.CreateElement("Info_2");//创建新的子节点
            info.SetAttribute("Name", "小李");//创建新子节点属性名和属性值
            info.SetAttribute("Age", "25");
            info.SetAttribute("Phone", "87654321");
            root.AppendChild(info);//将子节点按照创建顺序,添加到xml
            xml.AppendChild(root);
            xml.Save(localPath);//保存xml到路径位置
            Debug.Log("添加XML成功!");
        }
    }
}

3、XML文件----读取

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


public class ReadXML : MonoBehaviour {

    public string localPath;
	void Start ()
    {
        Readxml();
    }

    void Readxml()
    {
        localPath = Application.streamingAssetsPath + "/" + "MyXML.xml";
        if (File.Exists(localPath))
        {
            XmlDocument xml = new XmlDocument();
            xml.Load(localPath);//加载xml文件
            XmlNodeList nodeList = xml.SelectSingleNode("Data").ChildNodes;
            foreach (XmlElement xe in nodeList)
            {//遍历所有子节点
                if (xe.Name == "Info_2")
                {
                    Debug.Log(xe.GetAttribute("Name"));//获取Name属性值
                    Debug.Log(xe.GetAttribute("Age"));
                    Debug.Log(xe.GetAttribute("Phone"));
                }
            }
            Debug.Log("读取XML成功!" + xml.OuterXml);
        }
    }
}

4、XML文件----更新

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

public class UpdateXML : MonoBehaviour {

    public string localPath;
	void Start () {
        UpdateXml();
    }
    public void UpdateXml()
    {
        localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
        if (File.Exists(localPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(localPath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
            foreach (XmlElement xe in nodeList)
            {
                //拿到节点中属性Name =小王的节点
                if (xe.GetAttribute("Name") == "小王")
                {
                    //更新节点属性
                    xe.SetAttribute("Age", "21");
                    break;
                }
            }
            xmlDoc.Save(localPath);
            Debug.Log("更新XML成功!");
        }
    }
}

5、XML文件----删除

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

public class DeleteXML : MonoBehaviour {

    public string localPath;
	void Start () {
        DeleteXml();

    }

    public void DeleteXml()
    {
        localPath = UnityEngine.Application.streamingAssetsPath + "/" + "MyXML.xml";
        if (File.Exists(localPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(localPath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("Data").ChildNodes;
            foreach (XmlElement xe in nodeList)
            {
                if (xe.GetAttribute("Name") == "小李")
                {
                    xe.RemoveAttribute("Phone");
                }
                if (xe.GetAttribute("Name") == "小王")
                {
                    xe.RemoveAll();
                }
            }
            xmlDoc.Save(localPath);
            Debug.Log("删除XML成功!");
        }
    }
}

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值