Unity3D C# 读写XML

效果




代码

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

public class XMLTest : MonoBehaviour {

    private string _xmlPath;
    private string _userId = "";
    private string _userName = "";



	// Use this for initialization
	void Start () 
    {
        //xml路径
        _xmlPath = Application.dataPath + "/test.xml";
        CreatXML();
	}
	
	// Update is called once per frame
	void Update () 
    {
	
	}

    void CreatXML()
    {
        //检测xml是否存在
        if (!File.Exists(_xmlPath))
        {
            //新建xml实例
            XmlDocument xmlDoc = new XmlDocument();
            //创建根节点,最上层节点
            XmlElement data = xmlDoc.CreateElement("data");
            xmlDoc.AppendChild(data);
            //二级节点
            XmlElement user = xmlDoc.CreateElement("user");
            data.AppendChild(user);
            //二级节点的两个属性
             XmlElement userId = xmlDoc.CreateElement("userId");
             user.AppendChild(userId);
            XmlElement userName = xmlDoc.CreateElement("userName");
            user.AppendChild(userName);
            
            //将xml文件保存到本地
            xmlDoc.Save(_xmlPath);
            Debug.Log("xml creat success!");
        }
    }

    void OnGUI()
    {
        GUI.Button(new Rect(0, 0, 100, 50), "UserId");

        _userId = GUI.TextField(new Rect(100, 0, 100, 50), _userId);


        GUI.Button(new Rect(0, 50, 100, 50), "UserName");
        

        _userName = GUI.TextField(new Rect(100, 50, 100, 50), _userName);
        if(GUI.Button(new Rect(200,25,100,50),"更改"))
        {
            UpdateXml(_userId, _userName);
        }
        

        //GUI.Button(new Rect(0, 100, 150, 50), "UserId" + _userId);
        //GUI.Button(new Rect(0, 150, 150, 50), "UserName" + _userName);


    }

    void UpdateXml(string  userId,string userName)
    {
        if (File.Exists(_xmlPath))
        {
            XmlDocument xmlDoc = new XmlDocument();
            xmlDoc.Load(_xmlPath);
            XmlNodeList nodeList = xmlDoc.SelectSingleNode("data/user").ChildNodes;
            foreach (XmlElement xe in nodeList)
            {
                Debug.Log(nodeList.Count);
                if (xe.Name == "userId")
                {
                    xe.InnerText = userId;
                    Debug.Log("edit");
                    
                }
                if (xe.Name == "userName")
                {
                    xe.InnerText = userName;
                    break;
                }
            }
            xmlDoc.Save(_xmlPath);
        }
    }
}


  • 2
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 11
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 11
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

地狱为王

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值