XML的使用,增删改查(Unity中的)

前端之家收集整理的这篇文章主要介绍了XML的使用,增删改查(Unity中的)前端之家小编觉得挺不错的,现在分享给大家,也给大家做个参考。

创建之后的XML

<root_CharacterTmp>
  <CharacterTmp id="1000" name="xml">
    <JobID>2</JobID>
    <JobMode>none</JobMode>
    <InitForce>2.2</InitForce>
  </CharacterTmp>
  <CharacterTmp name="name1">
    <JobID>
    </JobID>
    <JobMode>none</JobMode>
    <InitForce>2.2</InitForce>
  </CharacterTmp>
</root_CharacterTmp>
创建代码


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

public class XmlTest : MonoBehavIoUr {

	string filePath;
	int id;
	int jobID;
	string jobMode;
	float initForce;

	void Start () {
		filePath = Application.dataPath + "/Test.xml";
	}

	void OnGUI()
	{
		if (GUI.Button (new Rect (10,10,200,30),"CREATE XML"))
			CreateXMl ();
		if (GUI.Button (new Rect (10,50,"UpDate XML"))
			UpDateXml ();
		if (GUI.Button (new Rect (10,90,"Add XML"))
			AddXml ();
		if (GUI.Button (new Rect (10,130,"Delete XML"))
			DeleteXml ();
		if (GUI.Button (new Rect (10,170,"Delete XML"))
			ShowXml ();

		GUILayout.Label ("id:" + id);

	}

	//创建XML
	public void CreateXMl()
	{
		//检测xml是否存在
		if(!File.Exists(filePath))
		{
			//新建XML实例
			XmlDocument xmlDoc = new XmlDocument();
			//创建根节点
			XmlElement root = 
				xmlDoc.CreateElement("root_CharacterTmp");
			//创建下一层节点
			XmlElement elmNew = 
				xmlDoc.CreateElement("CharacterTmp");
			//设置属性
			elmNew.SetAttribute("id","0");
			elmNew.SetAttribute("name","xml");
			//继续创建下一层节点
			XmlElement jobid = 
				xmlDoc.CreateElement("JobID");
			//设置节点的值
			jobid.InnerText = "1";
			XmlElement jobMode = 
				xmlDoc.CreateElement("JobMode");
			jobMode.InnerText = "none";
			XmlElement initForce = 
				xmlDoc.CreateElement("InitForce");
			initForce.InnerText = "0";
			//吧节点一层一层的添加
			elmNew.AppendChild(jobid);
			elmNew.AppendChild(jobMode);
			elmNew.AppendChild(initForce);
			root.AppendChild(elmNew);
			xmlDoc.AppendChild(root);
			xmlDoc.Save(filePath);
			Debug.Log("createXml ok!");
		}
	}

	//更新XML
	public void UpDateXml()
	{
		//检测xml是否存在
		if(File.Exists(filePath))
		{
			//新建实例
			XmlDocument xmlDoc = new XmlDocument();
			//根据路径将xml读取出来
			xmlDoc.Load(filePath);
			//得到根节点
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").ChildNodes;
			//遍历所有子节点
			foreach(XmlElement xe in nodeList)
			{
				//拿到节点中属性 id == 0的节点
				if(xe.GetAttribute("id") == "0")
				{
					//更新节点属性
					xe.SetAttribute("id","1000");
					//继续遍历
					foreach(XmlElement x1 in xe.ChildNodes)
					{
						if(x1.Name == "JobID")
						{
							//更新值
							x1.InnerText = "2";
						}
					}

				}
			}
			xmlDoc.Save(filePath);
			Debug.Log("UpDateXML OK!");
		}
	}

	//添加xml
	public void AddXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			//添加根节点
			XmlNode root = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp");
			//添加
			XmlElement elmNew = 
				xmlDoc.CreateElement("CharacterTmp");
			elmNew.SetAttribute("id","1");
			elmNew.SetAttribute("name","name1");
			XmlElement jobid = 
				xmlDoc.CreateElement("JobID");
			jobid.InnerText = "1";
			elmNew.AppendChild(jobid);
			XmlElement jobMode = 
				xmlDoc.CreateElement("JobMode");
			jobMode.InnerText = "none";
			elmNew.AppendChild(jobMode);
			XmlElement initForce = 
				xmlDoc.CreateElement("InitForce");
			initForce.InnerText = "2.2";
			elmNew.AppendChild(initForce);
			root.AppendChild(elmNew);
			xmlDoc.AppendChild(root);
			xmlDoc.Save(filePath);
			Debug.Log("AddXml OK!");

		}
	}

	//删除XML
	public void DeleteXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").ChildNodes;
			foreach(XmlElement xe in nodeList)
			{
				if(xe.GetAttribute("id") == "1")
				{
					xe.RemoveAttribute("id");
					foreach(XmlElement x1 in xe.ChildNodes)
					{
						if(x1.Name == "JobID")
						{
							x1.RemoveAll();
						}
					}
				}
			}
			xmlDoc.Save(filePath);
			Debug.Log("deleteXml OK!");
		}
	}

	//解析xml
	public void ShowXml()
	{
		if(File.Exists(filePath))
		{
			XmlDocument xmlDoc = new XmlDocument();
			xmlDoc.Load(filePath);
			XmlNodeList nodeList = 
				xmlDoc.SelectSingleNode
					("root_CharacterTmp").ChildNodes;
			foreach(XmlElement xe in nodeList)
			{
				if(xe.GetAttribute("id") == "1000")
				{

					id = int.Parse(xe.GetAttribute("id"));
					Debug.Log("id:" + id);
					foreach(XmlElement x1 in xe.ChildNodes)
					{
						switch(x1.Name)
						{
						case "JobID": 
							jobID = int.Parse(x1.InnerText);
							Debug.Log("jobID:" + jobID);
							break;
						case "JobMode" : 
							jobMode = x1.InnerText;
							Debug.Log("jobMode:" + jobMode);
							break;
						case "InitForce": 
							initForce = float.Parse(x1.InnerText);
							Debug.Log("initForce:" + initForce);
							break;
						default:
							break;
						}
					}

				}
			}
		}
	}

}

猜你在找的XML相关文章