Unity3D C# 读写XML
来源:互联网 发布:路由器网络参数设置 编辑:程序博客网 时间:2024/05/01 15:53
效果
代码
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 initializationvoid Start () { //xml路径 _xmlPath = Application.dataPath + "/test.xml"; CreatXML();}// Update is called once per framevoid 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); } }}
- Unity3D C# 读写XML
- Unity3D 读写XML文件
- Unity3D 读写XML文件
- Unity3d 读写XML文件
- unity3d读写XML
- unity3d读写XML文件
- unity3d XML配置表读写
- Unity3d如何读写XML文件
- unity3d C#文本读写操作
- c#读写xml文件
- c#读写xml文件
- c#读写xml文件
- C#读写xml文件
- c#读写xml文件
- C#读写xml文件
- c# 读写xml文件
- C#读写xml文件
- C#读写xml文件
- torque+maui问题-任务不运行
- 基于STSdb和fastJson的磁盘/内存缓存
- 设计模式之单例模式
- 修改VS属性页中的Inherited values
- cocos2d-x-2.1.4 环境搭建问题
- Unity3D C# 读写XML
- nginx框架 摘录
- Communication
- send email with attachment
- linux端口被占解决办法
- linux查看日志前1000行
- 今天起励志成为一名IT女民工,支持的请跟帖
- spin_lock_irq和spin_lock_irqsave
- 三天之感