使用Xml读取存储数据
0.原xml数据
<?xml version="1.0" encoding="UTF-8"?>
<PlayerInfo>
<name>shanJia</name>
<atk>10</atk>
<def>5</def>
<moveSpeed>20</moveSpeed>
<roundSpeed>20</roundSpeed>
<weapon>
<id>1</id>
<num>1</num>
</weapon>
<listInt>
<int>1</int>
<int>2</int>
<int>3</int>
</listInt>
<itemList>
<Item id="1" num="20"/>
<Item id="2" num="30"/>
<Item id="3" num="40"/>
</itemList>
<itemDic>
<int>1</int>
<Item id="1" num="1"/>
<int>2</int>
<Item id="2" num="2"/>
</itemDic>
</PlayerInfo>
1.读取
代码如下(示例):
using System.Collections;
using System.Collections.Generic;
using System.IO;
using System.Xml;
using UnityEngine;
public class Item
{
public int id;
public int num;
}
public class PlayerInfo
{
public string name;
public int atk;
public int def;
public float moveSpeed;
public float roundSpeed;
public Item weapon = new Item();
public List<int> listInt = new List<int>();
public List<Item> itemList = new List<Item>();
public Dictionary<int, Item> itemDic = new Dictionary<int, Item>();
public void LoadData(string fileName)
{
string path = Application.persistentDataPath + "/" + fileName + ".xml";
if(!File.Exists(path))
{
path = Application.streamingAssetsPath + "/" + fileName + ".xml";
}
XmlDocument xml = new XmlDocument();
xml.Load(path);
XmlNode root = xml.SelectSingleNode("PlayerInfo");
name = root.SelectSingleNode("name").InnerText;
atk = int.Parse(root.SelectSingleNode("atk").InnerText);
def = int.Parse(root.SelectSingleNode("def").InnerText);
moveSpeed = int.Parse(root.SelectSingleNode("moveSpeed").InnerText);
roundSpeed = int.Parse(root.SelectSingleNode("roundSpeed").InnerText);
weapon.id = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("id").InnerText);
weapon.num = int.Parse(root.SelectSingleNode("weapon").SelectSingleNode("num").InnerText);
XmlNode intList = root.SelectSingleNode("listInt");
XmlNodeList lsit = intList.SelectNodes("int");
for (int i = 0; i < lsit.Count; i++)
{
listInt.Add(int.Parse(lsit[i].InnerText));
}
XmlNodeList Itemlist = root.SelectSingleNode("itemList").SelectNodes("Item");
for (int i = 0; i < Itemlist.Count; i++)
{
Item t = new Item();
t.id = int.Parse(Itemlist[i].Attributes["id"].Value);
t.num = int.Parse(Itemlist[i].Attributes["num"].Value);
itemList.Add(t);
}
XmlNodeList dic = root.SelectSingleNode("itemDic").SelectNodes("Item");
foreach (XmlNode item in dic)
{
Item item1 = new Item();
item1.id = int.Parse(item.Attributes["id"].Value);
item1.num = int.Parse(item.Attributes["num"].Value);
itemDic.Add(item1.id, item1);
}
}
2.存储
代码如下(示例):
public void SaveData(string fileName)
{
string path = Application.persistentDataPath + "/" + fileName + ".xml";
Debug.Log(Application.persistentDataPath);
XmlDocument xml = new XmlDocument();
XmlDeclaration xmlDec = xml.CreateXmlDeclaration("1.0", "UTF-8", "");
xml.AppendChild(xmlDec);
XmlElement root = xml.CreateElement("PlayerInfo");
xml.AppendChild(root);
XmlElement name = xml.CreateElement("name");
name.InnerText = this.name;
root.AppendChild(name);
XmlElement atk = xml.CreateElement("atk");
atk.InnerText = this.atk.ToString();
root.AppendChild(atk);
XmlElement def = xml.CreateElement("def");
def.InnerText = this.def.ToString();
root.AppendChild(def);
XmlElement moveSpeed = xml.CreateElement("moveSpeed");
moveSpeed.InnerText = this.moveSpeed.ToString();
root.AppendChild(moveSpeed);
XmlElement roundSpeed = xml.CreateElement("roundSpeed");
roundSpeed.InnerText = this.roundSpeed.ToString();
root.AppendChild(roundSpeed);
XmlElement weapon = xml.CreateElement("weapon");
XmlElement id = xml.CreateElement("id");
id.InnerText = this.weapon.id.ToString();
XmlElement num = xml.CreateElement("num");
num.InnerText = this.weapon.num.ToString();
weapon.AppendChild(id);
weapon.AppendChild(num);
root.AppendChild(weapon);
XmlElement listInt = xml.CreateElement("listInt");
for (int i = 0; i < this.listInt.Count; i++)
{
XmlElement intNode = xml.CreateElement("int");
intNode.InnerText = this.listInt[i].ToString();
listInt.AppendChild(intNode);
}
root.AppendChild(listInt);
XmlElement itemList = xml.CreateElement("itemList");
for (int i = 0; i < this.itemList.Count; i++)
{
XmlElement itemNode = xml.CreateElement("Item");
itemNode.SetAttribute("id", this.itemList[i].id.ToString());
itemNode.SetAttribute("num", this.itemList[i].num.ToString());
itemList.AppendChild(itemNode);
}
root.AppendChild(itemList);
XmlElement itemDic = xml.CreateElement("itemDic");
foreach (int key in this.itemDic.Keys)
{
XmlElement intNode = xml.CreateElement("int");
intNode.InnerText = key.ToString();
itemDic.AppendChild(intNode);
XmlElement itemNode = xml.CreateElement("Item");
itemNode.SetAttribute("id", this.itemDic[key].id.ToString());
itemNode.SetAttribute("num", this.itemDic[key].num.ToString());
itemDic.AppendChild(itemNode);
}
root.AppendChild(itemDic);
//存储数据
xml.Save(path);
}