unity读写XML的相关操作
来源:互联网 发布:问道刷钱软件 编辑:程序博客网 时间:2024/06/14 02:51
读写主要用到了
然后在其他地方调用就也可以了
/// 面向对象的三大特征:封装,继承, 多态
XmlDocument 类
XmlDocument.GetElementsByTagName 方法 (String)
返回一个 XmlNodeList,它包含与指定 Name 匹配的所有子代元素的列表。
using System;using System.Xml;using UnityEngine;class CarXMLConfigure{public const string Root="Root";public const string NetworkConfigure="NetworkConfigure";public const string IP="IP";public const string FileName="config.xml";public const string StreamingAssets="/StreamingAssets/"; public const string NM = "nm"; }class XMLManagement{private XmlDocument doc;private XmlElement root;private string fileName;public XMLManagement(){doc=new XmlDocument();} //首先加载public bool LoadFile(string filePath){fileName=filePath;doc.Load(filePath);if(doc.InnerXml!=""){root=doc.DocumentElement;return true;}elsereturn false;}public void CreateXMLFile(string filePath) //name should contain .xml,for example "config.xml"{fileName = filePath;XmlDeclaration xmldecl;xmldecl = doc.CreateXmlDeclaration("1.0","gb2312",null);doc.AppendChild (xmldecl);root=doc.CreateElement(CarXMLConfigure.Root);doc.AppendChild(root);} //添加节点public void setAttribute(string attriName,string attriValue){XmlElement child=doc.CreateElement(CarXMLConfigure.NetworkConfigure);child.SetAttribute(attriName,attriValue);if(root==null){Console.WriteLine("the root is null,please create it or get it from an exist xml file!");return;}root.AppendChild(child); save();//添加节点的时候 需要保存 }public void save(){doc.Save(fileName);} // 获取节点,public string getIP(){ // XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure); //root.SelectNodes(CarXMLConfigure.NetworkConfigure);// string text="";foreach(XmlNode node in list){ XmlElement xmlnode = (XmlElement)node; Debug.Log(node.Value + node.InnerXml + " == " + xmlnode.Name+" == "+xmlnode.Value +" === "+node.LocalName); text = xmlnode.GetAttribute("IP11");// 如果跟节点相同。只能获取到第二个。。。} Debug.Log(list.Count+" "+text);return text;} public string getAaaryIP(int a) { XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure + a.ToString()); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute(CarXMLConfigure.IP+a.ToString()); } //Debug.Log(text + " " + CarXMLConfigure.IP + a.ToString()); return text; } public string getMus() { XmlNodeList list = root.GetElementsByTagName("MusConfig"); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute("Mus"); } return text; } public string getNum() { XmlNodeList list = root.GetElementsByTagName("OthConfig"); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute(CarXMLConfigure.NM); } return text; } public string getN() { XmlNodeList list = root.GetElementsByTagName("NetConfig"); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute("m"); } return text; } public string getNam() { XmlNodeList list = root.GetElementsByTagName("NamConfig"); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute("nam"); } return text; } public string GetIP(string s) { XmlNodeList list = root.GetElementsByTagName(s); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute(s); } return text; } public string GetIp_Name(int a) { XmlNodeList list = root.GetElementsByTagName(CarXMLConfigure.NetworkConfigure + a.ToString()); string text = ""; foreach (XmlNode node in list) { text = ((XmlElement)node).GetAttribute(CarXMLConfigure.IP + a.ToString()); text = text + "|" + ((XmlElement)node).GetAttribute("name"+a.ToString()); text = text + "|" + ((XmlElement)node).GetAttribute("num"+a.ToString()); } //Debug.Log(text + " " + CarXMLConfigure.IP + a.ToString()); return text; }}
然后在其他地方调用就也可以了
using UnityEngine;using System.Collections;public class ReadXML : MonoBehaviour { XMLManagement manager;// Use this for initializationvoid Start () { manager = new XMLManagement(); manager.LoadFile(Application.dataPath+CarXMLConfigure.StreamingAssets+CarXMLConfigure.FileName); string str = manager.getAaaryIP(1); //manager.getIP(); string str1 = manager.getIP(); manager.setAttribute("A","B");// 添加 Debug.Log(str +" === "+str1);}// Update is called once per framevoid Update () { }}
父类指针指向子类对象,然后调用父类方法
阅读全文
0 0
- unity读写XML的相关操作
- xml的读写操作
- XML的相关操作
- java xml的读写操作!
- XML文件的读写操作
- XML 文件的读写操作
- unity读写xml
- Unity 读写XML
- Unity读写XML文件
- dom4j操作xml的读写操作
- Java IO 文件读写相关的操作
- unity利用c#读写xml
- 读写xml文件操作的类
- c#中XML文件的读写操作
- 课后作业 XML文件的读写操作
- sqlserver 2005 xml字段的读写操作
- jdom对xml文件的读写操作
- Java 对xml文件的读写操作
- final
- Linux学习第十四篇--文件系统和目录树的关系
- 浏览器的标准模式和怪异模式以及他们的区别
- Your ApplicationContext is unlikely to start due to a @ComponentScan of the default package.
- ETL总结
- unity读写XML的相关操作
- 创建表空间和创建表过程分析
- 心跳之旅—iOS用手机摄像头检测心率(PPG)
- Important Knowledge Points
- Android开发之SQLite
- Codeforces Round #381 (Div. 1) B Alyona and a tree 树状数组
- Spring MVC 环境搭建(一)
- ffmpeg 一些命令
- python中执行linux命令(调用linux命令)