unity C# xml建立,獲取,修改,刪除(android同樣適用)
閒話不說,程式碼附上:
先看xml檔案:
<?xml version="1.0" encoding="utf-8"?>
<Player>
<Player00>
<Postion name="Postion">
<X>-9.726</X>
<Y>0.791</Y>
<Z>8.063321</Z>
</Postion>
<Level>1</Level>
<Exp>0</Exp >
<Hp>5</Hp>
</Player00>
</Player>
C#指令碼
using UnityEngine;
using System.Xml;
using System.Collections.Generic;
using System.IO;
using System;
public class LoadPlayerInfo : MonoBehaviour {
public static LoadPlayerInfo _loadplayerinfo;
//xml資訊
public string xmlName = "PlayerInfo" ;//xml檔名
public string rootNodeName = "Player";//根節點name
//申明xml物件
private static XmlDocument xmlDoc = new XmlDocument();
//申明src
private static string Path;
void Awake()
{
_loadplayerinfo = this;
CreatePath();
}
/// <summary>
/// 建立xml檔案,
/// </summary>
public void CreatePath()
{
Path = Application.persistentDataPath + "/"+xmlName+".xml";
if (!File.Exists(Path))
{
xmlDoc.LoadXml(((TextAsset)Resources.Load("Xml/"+xmlName)).text);
xmlDoc.Save(Path);//儲存xml檔案
Debug.Log("Create Success:"+ Path);
}
else
{
xmlDoc.Load(Path);
Debug.Log("Use Success:"+Path);
}
}
void Start()
{
//Dictionary<string, string> dic = new Dictionary<string, string>();
//dic.Add("X", "66");
//dic.Add("Y", "44");
//dic.Add("Z", "20");
//ChangeValue(1, "Postion", dic);
//Debug.Log(CreatePlayerNode(2));
//Debug.Log(GetValue(1, "Level")["Level"]);
//SaveValue(1, "Level", dic);
//Debug.Log(SelectAllNode("Level").Count);
//Debug.Log(DeteleNodes(2));
}
/// <summary>
/// 獲取資料
/// </summary>
/// <param name="playerId">為player的唯一編號</param>
/// <param name="nodeName">獲取的標籤的name</param>
/// <returns>返回獲取標籤的childnode的innertext和name,如果沒有返回null</returns>
public Dictionary<string ,string> GetValue(int playerId,string nodeName)
{
//沒有玩家資訊就新建
if (CanCreateNode(playerId))
{
CreatePlayerNode(playerId);
}
XmlNodeList xmlRoottList = xmlDoc.SelectSingleNode(rootNodeName).ChildNodes;
string playerName = rootNodeName + "0" + playerId;
foreach (XmlElement x3 in xmlRoottList)
{
if (x3.Name.Equals(playerName))
{
Dictionary<string, string> dictionary = new Dictionary<string, string>();
foreach (XmlElement x1 in x3)
{
if (x1.Name.Equals(nodeName))
{
if (x1.HasAttribute("name"))
{
foreach (XmlElement x2 in x1)
{
dictionary.Add(x2.Name, x2.InnerText);
Debug.Log(x2.Name + ":Get");
}
}
else
{
dictionary.Add(x1.Name, x1.InnerText);
}
Debug.Log("Get Success");
return dictionary;
}
}
}
}
return null;
}
/// <summary>
/// 修改資料
/// </summary>
/// <param name="playerId">player唯一id</param>
/// <param name="nodeName">需要修改的標籤name</param>
/// <param name="dictionary">輸入修改的值,如果標籤無子標籤,則字典的key為標籤的名字,否則為子標籤的名字</param>
/// <returns>返回是否修改成功</returns>
public bool ChangeValue(int playerId, string nodeName, Dictionary<string, string> dictionary)//儲存資料
{
//如果沒有玩家建立一個新的玩家
if (CanCreateNode(playerId))
{
Debug.Log("None");
CreatePlayerNode(playerId);
}
string playerName = rootNodeName + "0" + playerId;
XmlNodeList nodeRootList = xmlDoc.SelectSingleNode(rootNodeName).ChildNodes;
foreach(XmlElement x3 in nodeRootList)
{
if (x3.Name.Equals(playerName))
{
foreach (XmlElement x1 in x3)
{
if (x1.Name.Equals(nodeName))
{
if (dictionary.Count == 1)
{
x1.InnerText = dictionary[nodeName];
}
else if (dictionary.Count > 1)
{
foreach (XmlElement x2 in x1)
{
if (dictionary.ContainsKey(x2.Name))
{
x2.InnerText = dictionary[x2.Name];
}
}
}
}
}
}
}
try
{
xmlDoc.Save(Path);
Debug.Log("Change Success");
return true;
}
catch(Exception e)
{
Debug.Log("Change Error:"+e.Message);
return false;
}
}
/// <summary>
/// 建立一個新的player
/// </summary>
/// <param name="playerId">新player的唯一id</param>
/// <returns>返回是否建立成功(0.建立成功 1.建立失敗 2.已經有重複的物件)</returns>
public int CreatePlayerNode(int playerId)
{
if (CanCreateNode(playerId))
{
//最上層節點
XmlNode root = xmlDoc.SelectSingleNode(rootNodeName);//創尋找根節點
XmlElement newPlayerNode = xmlDoc.CreateElement(rootNodeName + "0" + playerId);
//遍歷第一層子節點所有name
List<string> rootList = SelectAllNode("root");
//第一層子節點
XmlElement childInRoot;
//接收值得dictionary
Dictionary<string, string> tempDic = new Dictionary<string, string>();
for (int i = 0; i < rootList.Count; i++)
{
//建立第一層子節點
childInRoot = xmlDoc.CreateElement(rootList[i]);
//遍歷第二層子節點所有節點名
List<string> childList = SelectAllNode(rootList[i]);
//Debug.Log(rootList[i]);
if (childList.Count == 0)//如果沒有子節點,輸入預設值
{
tempDic = GetValue(0, rootList[i]);
childInRoot.InnerText = tempDic[rootList[i]];
tempDic.Clear();
}
else//如果有子節點則再次遍歷
{
XmlElement child00;
childInRoot.SetAttribute("name", childInRoot.Name);//設定name
tempDic = GetValue(0, rootList[i]);
for (int j = 0; j < childList.Count; j++)
{
child00 = xmlDoc.CreateElement(childList[j]);
child00.InnerText =tempDic[childList[j]];//輸入預設值
childInRoot.AppendChild(child00);
}
tempDic.Clear();
}
newPlayerNode.AppendChild(childInRoot);
root.AppendChild(newPlayerNode);
}
//儲存
try
{
xmlDoc.Save(Path);
Debug.Log("Create Node Success:" + rootNodeName + "0" + playerId);
return 0;
}
catch (Exception e)
{
Debug.Log("Create Error:"+e.Message);
return 1;
}
}
else
{
return 2;
}
}
/// <summary>
/// 查詢子目錄下所有節點的名字
/// </summary>
/// <param name="nodeName">父節點名字</param>
/// <returns>返回所有節點的名字</returns>
public List<string> SelectAllNode(string nodeName)
{
List<string> nodeList = new List<string>();
XmlNodeList xmlList = xmlDoc.SelectSingleNode(rootNodeName).ChildNodes;//獲取該層上的所有子節點
foreach(XmlElement x3 in xmlList)
{
if (x3.Name.Equals(rootNodeName + "00"))
{
if (nodeName.Equals("root"))//返回最上層的的所有子節點
{
foreach (XmlElement x1 in x3)
{
nodeList.Add(x1.Name);
//Debug.Log(x1.Name);
}
return nodeList;
}
else//返回第二次的所有子節點
{
//Debug.Log(x3.Name);
foreach (XmlElement x1 in x3)
{
if (x1.Name.Equals(nodeName))
{
if (x1.HasAttributes)
{
foreach (XmlElement x2 in x1)
{
nodeList.Add(x2.Name);
}
}
}
}
return nodeList;
}
}
}
return null;
}
/// <summary>
/// 刪除player節點
/// </summary>
/// <param name="playerId">player唯一id</param>
/// <returns>返回刪除結果(0,刪除成功,2.無節點)</returns>
public int DeteleNodes(int playerId)
{
XmlNodeList rootList = xmlDoc.SelectSingleNode(rootNodeName).ChildNodes;
XmlElement root = xmlDoc.DocumentElement;
foreach(XmlElement x1 in rootList)
{
if (x1.Name.Equals(rootNodeName + "0" + playerId))
{
root.RemoveChild(x1);
xmlDoc.Save(Path);
Debug.Log("Detele Node Success");
return 0;
}
}
Debug.Log("Has Not Node");
return 2;
}
/// <summary>
/// 判斷是否有重複的player
/// </summary>
/// <param name="playerId">所需要判斷的playerId</param>
/// <returns>返回是否有重複的</returns>
public bool CanCreateNode(int playerId)
{
XmlNodeList playerNodeList = xmlDoc.SelectSingleNode(rootNodeName).ChildNodes;
foreach(XmlElement x1 in playerNodeList)
{
if (x1.Name.Equals(rootNodeName + "0" + playerId))
{
return false;
}
}
Debug.Log("Has Same Node Name");
return true;
}
}
現在可以使用了
注意:playerinfo.xml檔案需要放在Resources/Xml資料夾下,Resources/Xml只是為了在本地建立xml檔案,執行第一次之後就可以刪掉,android平臺同樣適用。
相關推薦
unity C# xml建立,獲取,修改,刪除(android同樣適用)
閒話不說,程式碼附上: 先看xml檔案: <?xml version="1.0" encoding="utf-8"?> <Player> <Player00> <Postion name="Posti
Zookeeper命令列操作 常用命令 客戶端連線 檢視znode路徑 建立節點 獲取znode資料,檢視節點內容,設定
8.1.常用命令 啟動ZK服務bin/zkServer.sh start 檢視ZK服務狀態bin/zkServer.sh status 停止ZK服務bin/zkServer.sh stop 重啟ZK服務bin/zkServer.sh restart 連線伺服器
嵌入式Linux併發程式設計,程序間通訊方式,無名管道,無名管道特點,無名管道建立pipe(),獲取管道大小,管道斷裂
1,Linux下的程序間通訊機制 Linux下的程序間通訊機制 應用 早期UNIX程序間通訊方式(很多是從Unix繼承的) 無名管道(pipe) 本地通訊,用於一臺計算機內部不同程序之間的通訊
對c:forEach的資料,雙擊修改,回車提交的實現
<c:forEach items="${student_list}" var="student" varStatus="status"> <tr> <td><in
js 的date的format時間,獲取當前時間,前一天的日期
mon class orm hour days sub lac reg .get Date.prototype.Format = function (fmt) { //author: meizz var o = { "M+": this.getM
ThinkPHP分類查詢(獲取當前分類的子分類,獲取父分類,下一級分類)
lod ids implode logs emp str foreach reac 初始化 獲取指定分類的所有子分類ID號 //獲取指定分類的所有子分類ID號 function getAllChildcateIds($categoryID){ //初始化ID數組
php利用simple_html_dom類,獲取頁面內容,充當爬蟲角色
contents names mac tro upd tool one mit 一個 PHP腳本扮演爬蟲的角色,可能大家第一時間想到可能會是會正則,個人對正則的規則老是記不住,表示比較難下手,今天工作中有個需求需要爬取某個網站上的一些門店信息 無意間在網上看到一個比較好的
利用jquery.ajax在jsp頁面動態生成table,可以增加修改,並支持一行和多行刪除
分享 ica 圖片 PE sibling 多行 點擊 table 技術 聲明:此為本人原創,只想實現功能,界面樣式方面沒多考慮,很粗糙能看懂就行……2018-5-14 動態生成table,我利用jsp內嵌java代碼從後臺獲取對象集合,輸出的時候有2中方法 1.直接利用
JS中使用時間戳,獲取當前日期,計算前一週的日期~
今天專案中用到了一點 隨便記錄一下 function timestampToTime(timestamp) { var date = new Date(timestamp * 1000);//時間戳為10位需*1000,時間戳為13位的話不需乘1000 var Y =
HDU-5692-Snacks(DFS序+線段樹,單點修改,區間查詢)
題目連結:http://acm.hdu.edu.cn/showproblem.php?pid=5692 Problem Description 百度科技園內有n 個零食機,零食機之間通過n−1 條路相互連通。每個零食機都有一個值v ,表示為小度熊提供零食的價值。 由於零
使用SpringMVC的crud操作時,進行資料修改,但是修改成功後,頁面無法顯示lastName屬性值(被修改的那條記錄)
我這個錯誤的原因在於,把map的鍵寫錯了,它必須和類名第一個字母小寫相同 @ModelAttribute public void getEmployee(@RequestParam(value="id",required=false) Integer id, Map&l
Python 程序,多程序,獲取程序id,給子程序傳遞引數
執行緒與執行緒之間共享全域性變數,程序之間不能共享全域性變數。 程序與程序相互獨立 (可以通過socket套接字實現程序間通訊,可以通過硬碟(檔案)實現程序通訊,也可以通過佇列(Queue)實現程序通訊) 子程序會拷貝複製主程序中的所有資源(變數、函式定義等),所以
小米紅米手機ROM製作工具,支援編輯修改,精簡app,定製化修改,小白也可上手。
ROM製作工具是目前windows環境下最強大的一款高效免費的ROM定製工具,使用這款工具可以幫助使用者製作修改線刷包、卡刷包智慧解包封包預裝,擁有多種專業ROM定製功能。支援小米、華為、vivo、oppo、一加、努比亞、中興、三星、酷派等品牌的ROM修改,除了製作rom,它
Java生成隨機數工具類,進位制之間的轉換工具類,獲取指定時間,時間格式轉換工具類
廢話不多說,貢獻一下code 1.編號生成工具 import org.apache.commons.lang3.StringUtils; import java.math.BigInteger; import java.text.SimpleDa
redis 全局命令 查看所有的鍵,刪除鍵,檢查鍵是否存在,獲取過期時間,鍵的數據結構類型
== exp table 結果 全局 ble str borde edi redis有5中數據結構,他們是鍵值對中的值,對於鍵來說,有一些通用的命令: 一、查看所有鍵 keys * 二、獲取鍵總數:dbsize 三、檢查鍵是否存在
redis 全域性命令 檢視所有的鍵,刪除鍵,檢查鍵是否存在,獲取過期時間,鍵的資料結構型別
redis有5中資料結構,他們是鍵值對中的值,對於鍵來說,有一些通用的命令: 一、檢視所有鍵 keys * 二、獲取鍵總數:dbsize 三、檢查鍵是否存在 exists 如果存在返回1,不存在返回0 四、刪除鍵 del ke
Python基礎(13):面向物件進階(訪問限制,__slots__,property,獲取物件資訊,類屬性和例項屬性)
一,訪問限制 原因:直接操作物件屬性有兩個缺點:無法保證資料安全性,無法進行引數校驗。 示例: class fruit(object): #定義一個類 def __init__(self,name): #定義屬性name
Python Flask,資料庫,SQLAlchemy,資料庫的修改、刪除
demo.py(資料庫的修改、刪除): from flask import Flask, current_app from flask_sqlalchemy import SQLAlchemy # 匯入 from demo1 import User # 匯入模型類 #
python爬蟲之反爬蟲(隨機user-agent,獲取代理ip,檢測代理ip可用性)
python爬蟲之反爬蟲(隨機user-agent,獲取代理ip,檢測代理ip可用性) 目錄 隨機User-Agent 獲取代理ip 檢測代理ip可用性 隨機User-Agent fake_useragent庫,偽
php 爬蟲的簡單實現, 獲取整個頁面, 再把頁面的資料匯入本地的檔案當中
$curlobj = curl_init(); //建立一個curl 的資源,下面要用的 curl_setopt($curlobj,CURLOPT_URL,"http://www.baidu.com