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

46 篇文章 0 订阅

创建之后的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, 200, 30), "UpDate XML"))
			UpDateXml ();
		if (GUI.Button (new Rect (10, 90, 200, 30), "Add XML"))
			AddXml ();
		if (GUI.Button (new Rect (10, 130, 200, 30), "Delete XML"))
			DeleteXml ();
		if (GUI.Button (new Rect (10, 170, 200, 30), "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;
						}
					}

				}
			}
		}
	}

}



  • 1
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
使用MySQL进行增删改查,需要在Unity使用MySQL Connector/NET驱动程序。下面是一个简单的Unity C#代码示例,说明如何连接到MySQL数据库并执行增删改查操作: 首先,需要在Unity安装MySQL Connector/NET驱动程序。可以从MySQL官方网站上下载。 然后,需要在C#项目添加对MySQL Connector/NET的引用。可以在Visual Studio右键单击项目,选择“添加引用”,然后选择MySQL Connector/NET。 接下来,需要在C#代码使用MySQL Connector/NET命名空间。可以使用以下代码: using MySql.Data.MySqlClient; 然后,需要创建一个MySQL连接对象,并将其连接到数据库。可以使用以下代码: string connectionString = "Server=myServerAddress;Database=myDataBase;Uid=myUsername;Pwd=myPassword;"; MySqlConnection connection = new MySqlConnection(connectionString); connection.Open(); 在这个代码,需要将“myServerAddress”替换为MySQL服务器的地址,“myDataBase”替换为要连接的数据库名称,“myUsername”替换为MySQL用户名,“myPassword”替换为MySQL密码。 接下来,可以使用MySQL命令对象执行SQL查询。可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "SELECT * FROM myTable"; MySqlDataReader reader = command.ExecuteReader(); while (reader.Read()) { Debug.Log(reader["columnName"].ToString()); } 在这个代码,需要将“myTable”替换为要查询的表名称,“columnName”替换为要检索的列名称。 要执行其他操作,如插入、更新或删除,可以使用类似的方法。 例如,要插入一条记录,可以使用以下代码: MySqlCommand command = connection.CreateCommand(); command.CommandText = "INSERT INTO myTable (columnName1, columnName2) VALUES ('value1', 'value2')"; command.ExecuteNonQuery(); 在这个代码,需要将“myTable”替换为要插入记录的表名称,“columnName1”和“columnName2”替换为要插入的列名称,“value1”和“value2”替换为要插入的值。 完成所有操作后,需要关闭MySQL连接。可以使用以下代码: connection.Close(); 这是一个简单的Unity使用MySQL进行增删改查的示例代码。注意,这只是一个起点,需要进一步学习MySQL Connector/NET和SQL查询语言才能深入了解如何使用MySQL进行增删改查
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值