关于Unity读取XML的简单学习
来源:互联网 发布:剑网3捏脸数据详细 编辑:程序博客网 时间:2024/05/17 23:16
本文转自 宣雨松 的博客 http://www.xuanyusong.com/archives/1901
using UnityEngine;using System.Collections;using System.Collections.Generic;using System.Xml;using System.IO;using System.Text;public class MyXML : MonoBehaviour { public void CreatXml() { //保存XML的路径,注意路径 string filePath = Application.dataPath + @"/"; //判断当前路径下是否有该文件 if (!File.Exists(filePath)) { //创建XML文档实例 XmlDocument xmlDoc = new XmlDocument(); //创建root节点,也就是最上层节点 XmlElement root = xmlDoc.CreateElement("transforms"); //继续创建下一个节点 XmlElement elmNew = xmlDoc.CreateElement("rotation"); //设置节点的两个参数 ID和NAME elmNew.SetAttribute("id", "0"); elmNew.SetAttribute("name", "eagle"); //继续创建下一层节点 XmlElement rotation_X = xmlDoc.CreateElement("x"); //设置节点中的数值 rotation_X.InnerText = "0"; XmlElement rotation_Y = xmlDoc.CreateElement("y"); rotation_Y.InnerText = "1"; XmlElement rotation_Z = xmlDoc.CreateElement("z"); rotation_Z.InnerText = "2"; //这里在添加一个节点属性,用来区分。。 rotation_Z.SetAttribute("id", "1"); //把节点一层层的添加至xmlDoc中,它们之间的先后顺序,这个就是生成XML文件的顺序 elmNew.AppendChild(rotation_X); elmNew.AppendChild(rotation_Y); elmNew.AppendChild(rotation_Z); root.AppendChild(elmNew); xmlDoc.AppendChild (root); //把XML保存至路径文件中 xmlDoc.Save(filePath); Debug.Log("成功创建"); } } public void UpdateXml() { string filePath = Application.dataPath + @"/"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument(); //根据路径把XML文件读出来 xmlDoc.Load(filePath); //得到transform下的所有节点 XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes; //遍历所有子节点 foreach (XmlElement xe in nodeList ) { //拿到节点中属性ID=0的节点 if (xe.GetAttribute("id") == "0") { //更新节点属性 xe.SetAttribute("id", "1000"); //继续遍历 foreach (XmlElement xl in xe.ChildNodes ) { if (xl.Name == "z") xl.InnerText = "update0000"; //这里是修改节点名称对应的数值,而上面的拿到节点连带的属性。 } break; } } xmlDoc.Save(filePath); Debug.Log("完成更新"); } } public void AddXml() { string filepath = Application.dataPath + @"/"; if (File.Exists(filepath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filepath); XmlNode root = xmlDoc.SelectSingleNode("transforms"); XmlElement elmNew = xmlDoc.CreateElement("rotation"); elmNew.SetAttribute("id", "1"); elmNew.SetAttribute("name", "赖张殷"); XmlElement rotation_X = xmlDoc.CreateElement("x"); rotation_X.InnerText = "0"; rotation_X.SetAttribute("id", "1"); XmlElement rotation_Y = xmlDoc.CreateElement("y"); rotation_Y.InnerText = "1"; XmlElement rotation_Z = xmlDoc.CreateElement("z"); rotation_Z.InnerText = "2"; elmNew.AppendChild(rotation_X); elmNew.AppendChild(rotation_Y); elmNew.AppendChild(rotation_Z); root.AppendChild(elmNew); xmlDoc.AppendChild(root); xmlDoc.Save(filepath); Debug.Log("加载完成"); } } public void DeleteXml() { string filePath = Application.dataPath + @"/"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes; foreach (XmlElement xe in nodeList ) { if (xe.GetAttribute("id") == "1") { xe.RemoveAttribute("id"); } foreach (XmlElement xl in xe.ChildNodes ) { if (xl.Name == "z") xl.RemoveAll(); } } xmlDoc.Save(filePath); Debug.Log("完成删除"); } } public void ShowXml() { string filePath = Application.dataPath + @"/"; if (File.Exists(filePath)) { XmlDocument xmlDoc = new XmlDocument(); xmlDoc.Load(filePath); XmlNodeList nodeList = xmlDoc.SelectSingleNode("transform").ChildNodes; //遍历每一个节点,拿节点的属性以及节点的内容 foreach (XmlElement xe in nodeList ) { Debug.Log("Attribute" + xe.GetAttribute("name")); Debug.Log("NAME" + xe.Name); foreach (XmlElement xl in xe.ChildNodes ) { if (xl.Name == "y") Debug.Log("VALUE:" + xl.InnerText); } } Debug.Log("ALL=" + xmlDoc.OuterXml); } }}
0 0
- 关于Unity读取XML的简单学习
- unity的XML读取
- Unity构建读取XML简单架构的方法与注意事项
- 关于Unity的几种XML读取方式
- 关于Java读取xml文件的学习
- 关于Java读取xml文件的学习
- 简单的 XML 读取
- 简单的xml读取
- 关于XML文档读取写入的简单操作
- unity学习笔记#3--在unity中读取XML数据
- 转:简单的 XML 读取
- 读取简单的xml文件
- 简单的XML文档读取
- 简单的读取xml格式文件
- 简单的读取xml文件
- Unity 读取xml 文件
- Unity 读取xml 文件
- unity c#读取xml
- 1.2 VS2005 Error LNK2001 无法解析的外部符号我自己的解决办法
- spring-data-redis increment 命令序列化失败
- android开发时R文件丢失解决方案
- Android NFC技术(三)——初次开发Android NFC你须知道NdefMessage和NdefRecord
- CFdiv2C
- 关于Unity读取XML的简单学习
- iOS 开发者必不可少的 75 个工具
- Java线程第二弹--资源冲突问题
- Android 快速开发系列 打造万能的ListView GridView 适配器
- public, protect, private定义变量的区别
- java反射获取某接口的所有实现类
- xcode插件入门教程
- 寻找下一个较大元素 --- 利用栈
- 基础之菜单项的使用